home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / gcc / gcc263_src.lha / gcc-2.6.3 / fold-const.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-01  |  150.5 KB  |  4,904 lines

  1. /* Fold a constant sub-tree into a single node for C-compiler
  2.    Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*@@ This file should be rewritten to use an arbitrary precision
  21.   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
  22.   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
  23.   @@ The routines that translate from the ap rep should
  24.   @@ warn if precision et. al. is lost.
  25.   @@ This would also make life easier when this technology is used
  26.   @@ for cross-compilers.  */
  27.  
  28.  
  29. /* The entry points in this file are fold, size_int and size_binop.
  30.  
  31.    fold takes a tree as argument and returns a simplified tree.
  32.  
  33.    size_binop takes a tree code for an arithmetic operation
  34.    and two operands that are trees, and produces a tree for the
  35.    result, assuming the type comes from `sizetype'.
  36.  
  37.    size_int takes an integer value, and creates a tree constant
  38.    with type from `sizetype'.  */
  39.    
  40. #include <stdio.h>
  41. #include <setjmp.h>
  42. #include "config.h"
  43. #include "flags.h"
  44. #include "tree.h"
  45.  
  46. /* Handle floating overflow for `const_binop'.  */
  47. static jmp_buf float_error;
  48.  
  49. static void encode    PROTO((HOST_WIDE_INT *, HOST_WIDE_INT, HOST_WIDE_INT));
  50. static void decode    PROTO((HOST_WIDE_INT *, HOST_WIDE_INT *, HOST_WIDE_INT *));
  51. int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
  52.                        HOST_WIDE_INT, HOST_WIDE_INT,
  53.                        HOST_WIDE_INT, HOST_WIDE_INT *,
  54.                        HOST_WIDE_INT *, HOST_WIDE_INT *,
  55.                        HOST_WIDE_INT *));
  56. static int split_tree    PROTO((tree, enum tree_code, tree *, tree *, int *));
  57. static tree const_binop PROTO((enum tree_code, tree, tree, int));
  58. static tree fold_convert PROTO((tree, tree));
  59. static enum tree_code invert_tree_comparison PROTO((enum tree_code));
  60. static enum tree_code swap_tree_comparison PROTO((enum tree_code));
  61. static int truth_value_p PROTO((enum tree_code));
  62. static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
  63. static int twoval_comparison_p PROTO((tree, tree *, tree *, int *));
  64. static tree eval_subst    PROTO((tree, tree, tree, tree, tree));
  65. static tree omit_one_operand PROTO((tree, tree, tree));
  66. static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
  67. static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
  68. static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
  69.                           tree, tree));
  70. static tree decode_field_reference PROTO((tree, int *, int *,
  71.                       enum machine_mode *, int *,
  72.                       int *, tree *));
  73. static int all_ones_mask_p PROTO((tree, int));
  74. static int simple_operand_p PROTO((tree));
  75. static tree range_test    PROTO((enum tree_code, tree, enum tree_code,
  76.                    enum tree_code, tree, tree, tree));
  77. static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
  78. static tree strip_compound_expr PROTO((tree, tree));
  79.  
  80. #ifndef BRANCH_COST
  81. #define BRANCH_COST 1
  82. #endif
  83.  
  84. /* Yield nonzero if a signed left shift of A by B bits overflows.  */
  85. #define left_shift_overflows(a, b)  ((a)  !=  ((a) << (b)) >> (b))
  86.  
  87. /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
  88.    Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
  89.    Then this yields nonzero if overflow occurred during the addition.
  90.    Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
  91.    Use `^' to test whether signs differ, and `< 0' to isolate the sign.  */
  92. #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
  93.  
  94. /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
  95.    We do that by representing the two-word integer in 4 words, with only
  96.    HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number.  */
  97.  
  98. #define LOWPART(x) \
  99.   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
  100. #define HIGHPART(x) \
  101.   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
  102. #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
  103.  
  104. /* Unpack a two-word integer into 4 words.
  105.    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
  106.    WORDS points to the array of HOST_WIDE_INTs.  */
  107.  
  108. static void
  109. encode (words, low, hi)
  110.      HOST_WIDE_INT *words;
  111.      HOST_WIDE_INT low, hi;
  112. {
  113.   words[0] = LOWPART (low);
  114.   words[1] = HIGHPART (low);
  115.   words[2] = LOWPART (hi);
  116.   words[3] = HIGHPART (hi);
  117. }
  118.  
  119. /* Pack an array of 4 words into a two-word integer.
  120.    WORDS points to the array of words.
  121.    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
  122.  
  123. static void
  124. decode (words, low, hi)
  125.      HOST_WIDE_INT *words;
  126.      HOST_WIDE_INT *low, *hi;
  127. {
  128.   *low = words[0] | words[1] * BASE;
  129.   *hi = words[2] | words[3] * BASE;
  130. }
  131.  
  132. /* Make the integer constant T valid for its type
  133.    by setting to 0 or 1 all the bits in the constant
  134.    that don't belong in the type.
  135.    Yield 1 if a signed overflow occurs, 0 otherwise.
  136.    If OVERFLOW is nonzero, a signed overflow has already occurred
  137.    in calculating T, so propagate it.
  138.  
  139.    Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
  140.    if it exists.  */
  141.  
  142. int
  143. force_fit_type (t, overflow)
  144.      tree t;
  145.      int overflow;
  146. {
  147.   HOST_WIDE_INT low, high;
  148.   register int prec;
  149.  
  150.   if (TREE_CODE (t) == REAL_CST)
  151.     {
  152. #ifdef CHECK_FLOAT_VALUE
  153.       CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
  154.              overflow);
  155. #endif
  156.       return overflow;
  157.     }
  158.  
  159.   else if (TREE_CODE (t) != INTEGER_CST)
  160.     return overflow;
  161.  
  162.   low = TREE_INT_CST_LOW (t);
  163.   high = TREE_INT_CST_HIGH (t);
  164.  
  165.   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  166.     prec = POINTER_SIZE;
  167.   else
  168.     prec = TYPE_PRECISION (TREE_TYPE (t));
  169.  
  170.   /* First clear all bits that are beyond the type's precision.  */
  171.  
  172.   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
  173.     ;
  174.   else if (prec > HOST_BITS_PER_WIDE_INT)
  175.     {
  176.       TREE_INT_CST_HIGH (t)
  177.     &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
  178.     }
  179.   else
  180.     {
  181.       TREE_INT_CST_HIGH (t) = 0;
  182.       if (prec < HOST_BITS_PER_WIDE_INT)
  183.     TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
  184.     }
  185.  
  186.   /* Unsigned types do not suffer sign extension or overflow.  */
  187.   if (TREE_UNSIGNED (TREE_TYPE (t)))
  188.     return 0;
  189.  
  190.   /* If the value's sign bit is set, extend the sign.  */
  191.   if (prec != 2 * HOST_BITS_PER_WIDE_INT
  192.       && (prec > HOST_BITS_PER_WIDE_INT
  193.       ? (TREE_INT_CST_HIGH (t)
  194.          & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
  195.       : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
  196.     {
  197.       /* Value is negative:
  198.      set to 1 all the bits that are outside this type's precision.  */
  199.       if (prec > HOST_BITS_PER_WIDE_INT)
  200.     {
  201.       TREE_INT_CST_HIGH (t)
  202.         |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
  203.     }
  204.       else
  205.     {
  206.       TREE_INT_CST_HIGH (t) = -1;
  207.       if (prec < HOST_BITS_PER_WIDE_INT)
  208.         TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
  209.     }
  210.     }
  211.  
  212.   /* Yield nonzero if signed overflow occurred.  */
  213.   return
  214.     ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
  215.      != 0);
  216. }
  217.  
  218. /* Add two doubleword integers with doubleword result.
  219.    Each argument is given as two `HOST_WIDE_INT' pieces.
  220.    One argument is L1 and H1; the other, L2 and H2.
  221.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  222.  
  223. int
  224. add_double (l1, h1, l2, h2, lv, hv)
  225.      HOST_WIDE_INT l1, h1, l2, h2;
  226.      HOST_WIDE_INT *lv, *hv;
  227. {
  228.   HOST_WIDE_INT l, h;
  229.  
  230.   l = l1 + l2;
  231.   h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
  232.  
  233.   *lv = l;
  234.   *hv = h;
  235.   return overflow_sum_sign (h1, h2, h);
  236. }
  237.  
  238. /* Negate a doubleword integer with doubleword result.
  239.    Return nonzero if the operation overflows, assuming it's signed.
  240.    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
  241.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  242.  
  243. int
  244. neg_double (l1, h1, lv, hv)
  245.      HOST_WIDE_INT l1, h1;
  246.      HOST_WIDE_INT *lv, *hv;
  247. {
  248.   if (l1 == 0)
  249.     {
  250.       *lv = 0;
  251.       *hv = - h1;
  252.       return (*hv & h1) < 0;
  253.     }
  254.   else
  255.     {
  256.       *lv = - l1;
  257.       *hv = ~ h1;
  258.       return 0;
  259.     }
  260. }
  261.  
  262. /* Multiply two doubleword integers with doubleword result.
  263.    Return nonzero if the operation overflows, assuming it's signed.
  264.    Each argument is given as two `HOST_WIDE_INT' pieces.
  265.    One argument is L1 and H1; the other, L2 and H2.
  266.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  267.  
  268. int
  269. mul_double (l1, h1, l2, h2, lv, hv)
  270.      HOST_WIDE_INT l1, h1, l2, h2;
  271.      HOST_WIDE_INT *lv, *hv;
  272. {
  273.   HOST_WIDE_INT arg1[4];
  274.   HOST_WIDE_INT arg2[4];
  275.   HOST_WIDE_INT prod[4 * 2];
  276.   register unsigned HOST_WIDE_INT carry;
  277.   register int i, j, k;
  278.   HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
  279.  
  280.   encode (arg1, l1, h1);
  281.   encode (arg2, l2, h2);
  282.  
  283.   bzero ((char *) prod, sizeof prod);
  284.  
  285.   for (i = 0; i < 4; i++)
  286.     {
  287.       carry = 0;
  288.       for (j = 0; j < 4; j++)
  289.     {
  290.       k = i + j;
  291.       /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
  292.       carry += arg1[i] * arg2[j];
  293.       /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
  294.       carry += prod[k];
  295.       prod[k] = LOWPART (carry);
  296.       carry = HIGHPART (carry);
  297.     }
  298.       prod[i + 4] = carry;
  299.     }
  300.  
  301.   decode (prod, lv, hv);    /* This ignores prod[4] through prod[4*2-1] */
  302.  
  303.   /* Check for overflow by calculating the top half of the answer in full;
  304.      it should agree with the low half's sign bit.  */
  305.   decode (prod+4, &toplow, &tophigh);
  306.   if (h1 < 0)
  307.     {
  308.       neg_double (l2, h2, &neglow, &neghigh);
  309.       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
  310.     }
  311.   if (h2 < 0)
  312.     {
  313.       neg_double (l1, h1, &neglow, &neghigh);
  314.       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
  315.     }
  316.   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
  317. }
  318.  
  319. /* Shift the doubleword integer in L1, H1 left by COUNT places
  320.    keeping only PREC bits of result.
  321.    Shift right if COUNT is negative.
  322.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  323.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  324.  
  325. void
  326. lshift_double (l1, h1, count, prec, lv, hv, arith)
  327.      HOST_WIDE_INT l1, h1, count;
  328.      int prec;
  329.      HOST_WIDE_INT *lv, *hv;
  330.      int arith;
  331. {
  332.   if (count < 0)
  333.     {
  334.       rshift_double (l1, h1, - count, prec, lv, hv, arith);
  335.       return;
  336.     }
  337.   
  338.   if (count >= prec)
  339.     count = (unsigned HOST_WIDE_INT) count & prec;
  340.  
  341.   if (count >= HOST_BITS_PER_WIDE_INT)
  342.     {
  343.       *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT;
  344.       *lv = 0;
  345.     }
  346.   else
  347.     {
  348.       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
  349.          | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1));
  350.       *lv = (unsigned HOST_WIDE_INT) l1 << count;
  351.     }
  352. }
  353.  
  354. /* Shift the doubleword integer in L1, H1 right by COUNT places
  355.    keeping only PREC bits of result.  COUNT must be positive.
  356.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  357.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  358.  
  359. void
  360. rshift_double (l1, h1, count, prec, lv, hv, arith)
  361.      HOST_WIDE_INT l1, h1, count;
  362.      int prec;
  363.      HOST_WIDE_INT *lv, *hv;
  364.      int arith;
  365. {
  366.   unsigned HOST_WIDE_INT signmask;
  367.   signmask = (arith
  368.           ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
  369.           : 0);
  370.  
  371.   if (count >= prec)
  372.     count = (unsigned HOST_WIDE_INT) count % prec;
  373.  
  374.   if (count >= HOST_BITS_PER_WIDE_INT)
  375.     {
  376.       *hv = signmask;
  377.       *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1)
  378.          | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT));
  379.     }
  380.   else
  381.     {
  382.       *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
  383.          | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1));
  384.       *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count)
  385.          | ((unsigned HOST_WIDE_INT) h1 >> count));
  386.     }
  387. }
  388.  
  389. /* Rotate the doubleword integer in L1, H1 left by COUNT places
  390.    keeping only PREC bits of result.
  391.    Rotate right if COUNT is negative.
  392.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  393.  
  394. void
  395. lrotate_double (l1, h1, count, prec, lv, hv)
  396.      HOST_WIDE_INT l1, h1, count;
  397.      int prec;
  398.      HOST_WIDE_INT *lv, *hv;
  399. {
  400.   HOST_WIDE_INT arg1[4];
  401.   register int i;
  402.   register int carry;
  403.  
  404.   if (count < 0)
  405.     {
  406.       rrotate_double (l1, h1, - count, prec, lv, hv);
  407.       return;
  408.     }
  409.  
  410.   encode (arg1, l1, h1);
  411.  
  412.   if (count > prec)
  413.     count = prec;
  414.  
  415.   carry = arg1[4 - 1] >> 16 - 1;
  416.   while (count > 0)
  417.     {
  418.       for (i = 0; i < 4; i++)
  419.     {
  420.       carry += arg1[i] << 1;
  421.       arg1[i] = LOWPART (carry);
  422.       carry = HIGHPART (carry);
  423.     }
  424.       count--;
  425.     }
  426.  
  427.   decode (arg1, lv, hv);
  428. }
  429.  
  430. /* Rotate the doubleword integer in L1, H1 left by COUNT places
  431.    keeping only PREC bits of result.  COUNT must be positive.
  432.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  433.  
  434. void
  435. rrotate_double (l1, h1, count, prec, lv, hv)
  436.      HOST_WIDE_INT l1, h1, count;
  437.      int prec;
  438.      HOST_WIDE_INT *lv, *hv;
  439. {
  440.   HOST_WIDE_INT arg1[4];
  441.   register int i;
  442.   register int carry;
  443.  
  444.   encode (arg1, l1, h1);
  445.  
  446.   if (count > prec)
  447.     count = prec;
  448.  
  449.   carry = arg1[0] & 1;
  450.   while (count > 0)
  451.     {
  452.       for (i = 4 - 1; i >= 0; i--)
  453.     {
  454.       carry *= BASE;
  455.       carry += arg1[i];
  456.       arg1[i] = LOWPART (carry >> 1);
  457.     }
  458.       count--;
  459.     }
  460.  
  461.   decode (arg1, lv, hv);
  462. }
  463.  
  464. /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
  465.    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  466.    CODE is a tree code for a kind of division, one of
  467.    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
  468.    or EXACT_DIV_EXPR
  469.    It controls how the quotient is rounded to a integer.
  470.    Return nonzero if the operation overflows.
  471.    UNS nonzero says do unsigned division.  */
  472.  
  473. int
  474. div_and_round_double (code, uns,
  475.               lnum_orig, hnum_orig, lden_orig, hden_orig,
  476.               lquo, hquo, lrem, hrem)
  477.      enum tree_code code;
  478.      int uns;
  479.      HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
  480.      HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
  481.      HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
  482. {
  483.   int quo_neg = 0;
  484.   HOST_WIDE_INT num[4 + 1];    /* extra element for scaling.  */
  485.   HOST_WIDE_INT den[4], quo[4];
  486.   register int i, j;
  487.   unsigned HOST_WIDE_INT work;
  488.   register int carry = 0;
  489.   HOST_WIDE_INT lnum = lnum_orig;
  490.   HOST_WIDE_INT hnum = hnum_orig;
  491.   HOST_WIDE_INT lden = lden_orig;
  492.   HOST_WIDE_INT hden = hden_orig;
  493.   int overflow = 0;
  494.  
  495.   if ((hden == 0) && (lden == 0))
  496.     abort ();
  497.  
  498.   /* calculate quotient sign and convert operands to unsigned.  */
  499.   if (!uns) 
  500.     {
  501.       if (hnum < 0)
  502.     {
  503.       quo_neg = ~ quo_neg;
  504.       /* (minimum integer) / (-1) is the only overflow case.  */
  505.       if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
  506.         overflow = 1;
  507.     }
  508.       if (hden < 0) 
  509.     {
  510.       quo_neg = ~ quo_neg;
  511.       neg_double (lden, hden, &lden, &hden);
  512.     }
  513.     }
  514.  
  515.   if (hnum == 0 && hden == 0)
  516.     {                /* single precision */
  517.       *hquo = *hrem = 0;
  518.       /* This unsigned division rounds toward zero.  */
  519.       *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
  520.       goto finish_up;
  521.     }
  522.  
  523.   if (hnum == 0)
  524.     {                /* trivial case: dividend < divisor */
  525.       /* hden != 0 already checked.  */
  526.       *hquo = *lquo = 0;
  527.       *hrem = hnum;
  528.       *lrem = lnum;
  529.       goto finish_up;
  530.     }
  531.  
  532.   bzero ((char *) quo, sizeof quo);
  533.  
  534.   bzero ((char *) num, sizeof num);    /* to zero 9th element */
  535.   bzero ((char *) den, sizeof den);
  536.  
  537.   encode (num, lnum, hnum); 
  538.   encode (den, lden, hden);
  539.  
  540.   /* Special code for when the divisor < BASE.  */
  541.   if (hden == 0 && lden < BASE)
  542.     {
  543.       /* hnum != 0 already checked.  */
  544.       for (i = 4 - 1; i >= 0; i--)
  545.     {
  546.       work = num[i] + carry * BASE;
  547.       quo[i] = work / (unsigned HOST_WIDE_INT) lden;
  548.       carry = work % (unsigned HOST_WIDE_INT) lden;
  549.     }
  550.     }
  551.   else
  552.     {
  553.       /* Full double precision division,
  554.      with thanks to Don Knuth's "Seminumerical Algorithms".  */
  555.     int quo_est, scale, num_hi_sig, den_hi_sig;
  556.  
  557.     /* Find the highest non-zero divisor digit.  */
  558.     for (i = 4 - 1; ; i--)
  559.       if (den[i] != 0) {
  560.     den_hi_sig = i;
  561.     break;
  562.       }
  563.  
  564.     /* Insure that the first digit of the divisor is at least BASE/2.
  565.        This is required by the quotient digit estimation algorithm.  */
  566.  
  567.     scale = BASE / (den[den_hi_sig] + 1);
  568.     if (scale > 1) {        /* scale divisor and dividend */
  569.       carry = 0;
  570.       for (i = 0; i <= 4 - 1; i++) {
  571.     work = (num[i] * scale) + carry;
  572.     num[i] = LOWPART (work);
  573.     carry = HIGHPART (work);
  574.       } num[4] = carry;
  575.       carry = 0;
  576.       for (i = 0; i <= 4 - 1; i++) {
  577.     work = (den[i] * scale) + carry;
  578.     den[i] = LOWPART (work);
  579.     carry = HIGHPART (work);
  580.     if (den[i] != 0) den_hi_sig = i;
  581.       }
  582.     }
  583.  
  584.     num_hi_sig = 4;
  585.  
  586.     /* Main loop */
  587.     for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
  588.       /* guess the next quotient digit, quo_est, by dividing the first
  589.      two remaining dividend digits by the high order quotient digit.
  590.      quo_est is never low and is at most 2 high.  */
  591.       unsigned HOST_WIDE_INT tmp;
  592.  
  593.       num_hi_sig = i + den_hi_sig + 1;
  594.       work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
  595.       if (num[num_hi_sig] != den[den_hi_sig])
  596.     quo_est = work / den[den_hi_sig];
  597.       else
  598.     quo_est = BASE - 1;
  599.  
  600.       /* refine quo_est so it's usually correct, and at most one high.   */
  601.       tmp = work - quo_est * den[den_hi_sig];
  602.       if (tmp < BASE
  603.       && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
  604.     quo_est--;
  605.  
  606.       /* Try QUO_EST as the quotient digit, by multiplying the
  607.          divisor by QUO_EST and subtracting from the remaining dividend.
  608.      Keep in mind that QUO_EST is the I - 1st digit.  */
  609.  
  610.       carry = 0;
  611.       for (j = 0; j <= den_hi_sig; j++)
  612.     {
  613.       work = quo_est * den[j] + carry;
  614.       carry = HIGHPART (work);
  615.       work = num[i + j] - LOWPART (work);
  616.       num[i + j] = LOWPART (work);
  617.       carry += HIGHPART (work) != 0;
  618.     }
  619.  
  620.       /* if quo_est was high by one, then num[i] went negative and
  621.      we need to correct things.  */
  622.  
  623.       if (num[num_hi_sig] < carry)
  624.     {
  625.       quo_est--;
  626.       carry = 0;        /* add divisor back in */
  627.       for (j = 0; j <= den_hi_sig; j++)
  628.         {
  629.           work = num[i + j] + den[j] + carry;
  630.           carry = HIGHPART (work);
  631.           num[i + j] = LOWPART (work);
  632.         }
  633.       num [num_hi_sig] += carry;
  634.     }
  635.  
  636.       /* store the quotient digit.  */
  637.       quo[i] = quo_est;
  638.     }
  639.   }
  640.  
  641.   decode (quo, lquo, hquo);
  642.  
  643.  finish_up:
  644.   /* if result is negative, make it so.  */
  645.   if (quo_neg)
  646.     neg_double (*lquo, *hquo, lquo, hquo);
  647.  
  648.   /* compute trial remainder:  rem = num - (quo * den)  */
  649.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  650.   neg_double (*lrem, *hrem, lrem, hrem);
  651.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  652.  
  653.   switch (code)
  654.     {
  655.     case TRUNC_DIV_EXPR:
  656.     case TRUNC_MOD_EXPR:    /* round toward zero */
  657.     case EXACT_DIV_EXPR:    /* for this one, it shouldn't matter */
  658.       return overflow;
  659.  
  660.     case FLOOR_DIV_EXPR:
  661.     case FLOOR_MOD_EXPR:    /* round toward negative infinity */
  662.       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
  663.     {
  664.       /* quo = quo - 1;  */
  665.       add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
  666.               lquo, hquo);
  667.     }
  668.       else return overflow;
  669.       break;
  670.  
  671.     case CEIL_DIV_EXPR:
  672.     case CEIL_MOD_EXPR:        /* round toward positive infinity */
  673.       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
  674.     {
  675.       add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  676.               lquo, hquo);
  677.     }
  678.       else return overflow;
  679.       break;
  680.     
  681.     case ROUND_DIV_EXPR:
  682.     case ROUND_MOD_EXPR:    /* round to closest integer */
  683.       {
  684.     HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
  685.     HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
  686.  
  687.     /* get absolute values */
  688.     if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
  689.     if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
  690.  
  691.     /* if (2 * abs (lrem) >= abs (lden)) */
  692.     mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
  693.             labs_rem, habs_rem, <wice, &htwice);
  694.     if (((unsigned HOST_WIDE_INT) habs_den
  695.          < (unsigned HOST_WIDE_INT) htwice)
  696.         || (((unsigned HOST_WIDE_INT) habs_den
  697.          == (unsigned HOST_WIDE_INT) htwice)
  698.         && ((HOST_WIDE_INT unsigned) labs_den
  699.             < (unsigned HOST_WIDE_INT) ltwice)))
  700.       {
  701.         if (*hquo < 0)
  702.           /* quo = quo - 1;  */
  703.           add_double (*lquo, *hquo,
  704.               (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
  705.         else
  706.           /* quo = quo + 1; */
  707.           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  708.               lquo, hquo);
  709.       }
  710.     else return overflow;
  711.       }
  712.       break;
  713.  
  714.     default:
  715.       abort ();
  716.     }
  717.  
  718.   /* compute true remainder:  rem = num - (quo * den)  */
  719.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  720.   neg_double (*lrem, *hrem, lrem, hrem);
  721.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  722.   return overflow;
  723. }
  724.  
  725. #ifndef REAL_ARITHMETIC
  726. /* Effectively truncate a real value to represent the nearest possible value
  727.    in a narrower mode.  The result is actually represented in the same data
  728.    type as the argument, but its value is usually different.
  729.  
  730.    A trap may occur during the FP operations and it is the responsibility
  731.    of the calling function to have a handler established.  */
  732.  
  733. REAL_VALUE_TYPE
  734. real_value_truncate (mode, arg)
  735.      enum machine_mode mode;
  736.      REAL_VALUE_TYPE arg;
  737. {
  738.   return REAL_VALUE_TRUNCATE (mode, arg);
  739. }
  740.  
  741. #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
  742.  
  743. /* Check for infinity in an IEEE double precision number.  */
  744.  
  745. int
  746. target_isinf (x)
  747.      REAL_VALUE_TYPE x;
  748. {
  749.   /* The IEEE 64-bit double format.  */
  750.   union {
  751.     REAL_VALUE_TYPE d;
  752.     struct {
  753.       unsigned sign      :  1;
  754.       unsigned exponent  : 11;
  755.       unsigned mantissa1 : 20;
  756.       unsigned mantissa2;
  757.     } little_endian;
  758.     struct {
  759.       unsigned mantissa2;
  760.       unsigned mantissa1 : 20;
  761.       unsigned exponent  : 11;
  762.       unsigned sign      :  1;
  763.     } big_endian;    
  764.   } u;
  765.  
  766.   u.d = dconstm1;
  767.   if (u.big_endian.sign == 1)
  768.     {
  769.       u.d = x;
  770.       return (u.big_endian.exponent == 2047
  771.           && u.big_endian.mantissa1 == 0
  772.           && u.big_endian.mantissa2 == 0);
  773.     }
  774.   else
  775.     {
  776.       u.d = x;
  777.       return (u.little_endian.exponent == 2047
  778.           && u.little_endian.mantissa1 == 0
  779.           && u.little_endian.mantissa2 == 0);
  780.     }
  781. }
  782.  
  783. /* Check whether an IEEE double precision number is a NaN.  */
  784.  
  785. int
  786. target_isnan (x)
  787.      REAL_VALUE_TYPE x;
  788. {
  789.   /* The IEEE 64-bit double format.  */
  790.   union {
  791.     REAL_VALUE_TYPE d;
  792.     struct {
  793.       unsigned sign      :  1;
  794.       unsigned exponent  : 11;
  795.       unsigned mantissa1 : 20;
  796.       unsigned mantissa2;
  797.     } little_endian;
  798.     struct {
  799.       unsigned mantissa2;
  800.       unsigned mantissa1 : 20;
  801.       unsigned exponent  : 11;
  802.       unsigned sign      :  1;
  803.     } big_endian;    
  804.   } u;
  805.  
  806.   u.d = dconstm1;
  807.   if (u.big_endian.sign == 1)
  808.     {
  809.       u.d = x;
  810.       return (u.big_endian.exponent == 2047
  811.           && (u.big_endian.mantissa1 != 0
  812.           || u.big_endian.mantissa2 != 0));
  813.     }
  814.   else
  815.     {
  816.       u.d = x;
  817.       return (u.little_endian.exponent == 2047
  818.           && (u.little_endian.mantissa1 != 0
  819.           || u.little_endian.mantissa2 != 0));
  820.     }
  821. }
  822.  
  823. /* Check for a negative IEEE double precision number.  */
  824.  
  825. int
  826. target_negative (x)
  827.      REAL_VALUE_TYPE x;
  828. {
  829.   /* The IEEE 64-bit double format.  */
  830.   union {
  831.     REAL_VALUE_TYPE d;
  832.     struct {
  833.       unsigned sign      :  1;
  834.       unsigned exponent  : 11;
  835.       unsigned mantissa1 : 20;
  836.       unsigned mantissa2;
  837.     } little_endian;
  838.     struct {
  839.       unsigned mantissa2;
  840.       unsigned mantissa1 : 20;
  841.       unsigned exponent  : 11;
  842.       unsigned sign      :  1;
  843.     } big_endian;    
  844.   } u;
  845.  
  846.   u.d = dconstm1;
  847.   if (u.big_endian.sign == 1)
  848.     {
  849.       u.d = x;
  850.       return u.big_endian.sign;
  851.     }
  852.   else
  853.     {
  854.       u.d = x;
  855.       return u.little_endian.sign;
  856.     }
  857. }
  858. #else /* Target not IEEE */
  859.  
  860. /* Let's assume other float formats don't have infinity.
  861.    (This can be overridden by redefining REAL_VALUE_ISINF.)  */
  862.  
  863. target_isinf (x)
  864.      REAL_VALUE_TYPE x;
  865. {
  866.   return 0;
  867. }
  868.  
  869. /* Let's assume other float formats don't have NaNs.
  870.    (This can be overridden by redefining REAL_VALUE_ISNAN.)  */
  871.  
  872. target_isnan (x)
  873.      REAL_VALUE_TYPE x;
  874. {
  875.   return 0;
  876. }
  877.  
  878. /* Let's assume other float formats don't have minus zero.
  879.    (This can be overridden by redefining REAL_VALUE_NEGATIVE.)  */
  880.  
  881. target_negative (x)
  882.      REAL_VALUE_TYPE x;
  883. {
  884.   return x < 0;
  885. }
  886. #endif /* Target not IEEE */
  887. #endif /* no REAL_ARITHMETIC */
  888.  
  889. /* Split a tree IN into a constant and a variable part
  890.    that could be combined with CODE to make IN.
  891.    CODE must be a commutative arithmetic operation.
  892.    Store the constant part into *CONP and the variable in &VARP.
  893.    Return 1 if this was done; zero means the tree IN did not decompose
  894.    this way.
  895.  
  896.    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
  897.    Therefore, we must tell the caller whether the variable part
  898.    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
  899.    The value stored is the coefficient for the variable term.
  900.    The constant term we return should always be added;
  901.    we negate it if necessary.  */
  902.  
  903. static int
  904. split_tree (in, code, varp, conp, varsignp)
  905.      tree in;
  906.      enum tree_code code;
  907.      tree *varp, *conp;
  908.      int *varsignp;
  909. {
  910.   register tree outtype = TREE_TYPE (in);
  911.   *varp = 0;
  912.   *conp = 0;
  913.  
  914.   /* Strip any conversions that don't change the machine mode.  */
  915.   while ((TREE_CODE (in) == NOP_EXPR
  916.       || TREE_CODE (in) == CONVERT_EXPR)
  917.      && (TYPE_MODE (TREE_TYPE (in))
  918.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
  919.     in = TREE_OPERAND (in, 0);
  920.  
  921.   if (TREE_CODE (in) == code
  922.       || (! FLOAT_TYPE_P (TREE_TYPE (in))
  923.       /* We can associate addition and subtraction together
  924.          (even though the C standard doesn't say so)
  925.          for integers because the value is not affected.
  926.          For reals, the value might be affected, so we can't.  */
  927.       && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
  928.           || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
  929.     {
  930.       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
  931.       if (code == INTEGER_CST)
  932.     {
  933.       *conp = TREE_OPERAND (in, 0);
  934.       *varp = TREE_OPERAND (in, 1);
  935.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  936.           && TREE_TYPE (*varp) != outtype)
  937.         *varp = convert (outtype, *varp);
  938.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  939.       return 1;
  940.     }
  941.       if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
  942.     {
  943.       *conp = TREE_OPERAND (in, 1);
  944.       *varp = TREE_OPERAND (in, 0);
  945.       *varsignp = 1;
  946.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  947.           && TREE_TYPE (*varp) != outtype)
  948.         *varp = convert (outtype, *varp);
  949.       if (TREE_CODE (in) == MINUS_EXPR)
  950.         {
  951.           /* If operation is subtraction and constant is second,
  952.          must negate it to get an additive constant.
  953.          And this cannot be done unless it is a manifest constant.
  954.          It could also be the address of a static variable.
  955.          We cannot negate that, so give up.  */
  956.           if (TREE_CODE (*conp) == INTEGER_CST)
  957.         /* Subtracting from integer_zero_node loses for long long.  */
  958.         *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
  959.           else
  960.         return 0;
  961.         }
  962.       return 1;
  963.     }
  964.       if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
  965.     {
  966.       *conp = TREE_OPERAND (in, 0);
  967.       *varp = TREE_OPERAND (in, 1);
  968.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  969.           && TREE_TYPE (*varp) != outtype)
  970.         *varp = convert (outtype, *varp);
  971.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  972.       return 1;
  973.     }
  974.     }
  975.   return 0;
  976. }
  977.  
  978. /* Combine two constants NUM and ARG2 under operation CODE
  979.    to produce a new constant.
  980.    We assume ARG1 and ARG2 have the same data type,
  981.    or at least are the same kind of constant and the same machine mode.
  982.  
  983.    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
  984.  
  985. static tree
  986. const_binop (code, arg1, arg2, notrunc)
  987.      enum tree_code code;
  988.      register tree arg1, arg2;
  989.      int notrunc;
  990. {
  991.   if (TREE_CODE (arg1) == INTEGER_CST)
  992.     {
  993.       register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
  994.       register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
  995.       HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
  996.       HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
  997.       HOST_WIDE_INT low, hi;
  998.       HOST_WIDE_INT garbagel, garbageh;
  999.       register tree t;
  1000.       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
  1001.       int overflow = 0;
  1002.  
  1003.       switch (code)
  1004.     {
  1005.     case BIT_IOR_EXPR:
  1006.       t = build_int_2 (int1l | int2l, int1h | int2h);
  1007.       break;
  1008.  
  1009.     case BIT_XOR_EXPR:
  1010.       t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
  1011.       break;
  1012.  
  1013.     case BIT_AND_EXPR:
  1014.       t = build_int_2 (int1l & int2l, int1h & int2h);
  1015.       break;
  1016.  
  1017.     case BIT_ANDTC_EXPR:
  1018.       t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
  1019.       break;
  1020.  
  1021.     case RSHIFT_EXPR:
  1022.       int2l = - int2l;
  1023.     case LSHIFT_EXPR:
  1024.       /* It's unclear from the C standard whether shifts can overflow.
  1025.          The following code ignores overflow; perhaps a C standard
  1026.          interpretation ruling is needed.  */
  1027.       lshift_double (int1l, int1h, int2l,
  1028.              TYPE_PRECISION (TREE_TYPE (arg1)),
  1029.              &low, &hi,
  1030.              !uns);
  1031.       t = build_int_2 (low, hi);
  1032.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1033.       if (!notrunc)
  1034.         force_fit_type (t, 0);
  1035.       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
  1036.       TREE_CONSTANT_OVERFLOW (t)
  1037.         = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
  1038.       return t;
  1039.  
  1040.     case RROTATE_EXPR:
  1041.       int2l = - int2l;
  1042.     case LROTATE_EXPR:
  1043.       lrotate_double (int1l, int1h, int2l,
  1044.               TYPE_PRECISION (TREE_TYPE (arg1)),
  1045.               &low, &hi);
  1046.       t = build_int_2 (low, hi);
  1047.       break;
  1048.  
  1049.     case PLUS_EXPR:
  1050.       if (int1h == 0)
  1051.         {
  1052.           int2l += int1l;
  1053.           if ((unsigned HOST_WIDE_INT) int2l < int1l)
  1054.         {
  1055.           hi = int2h++;
  1056.           overflow = int2h < hi;
  1057.         }
  1058.           t = build_int_2 (int2l, int2h);
  1059.           break;
  1060.         }
  1061.       if (int2h == 0)
  1062.         {
  1063.           int1l += int2l;
  1064.           if ((unsigned HOST_WIDE_INT) int1l < int2l)
  1065.         {
  1066.           hi = int1h++;
  1067.           overflow = int1h < hi;
  1068.         }
  1069.           t = build_int_2 (int1l, int1h);
  1070.           break;
  1071.         }
  1072.       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
  1073.       t = build_int_2 (low, hi);
  1074.       break;
  1075.  
  1076.     case MINUS_EXPR:
  1077.       if (int2h == 0 && int2l == 0)
  1078.         {
  1079.           t = build_int_2 (int1l, int1h);
  1080.           break;
  1081.         }
  1082.       neg_double (int2l, int2h, &low, &hi);
  1083.       add_double (int1l, int1h, low, hi, &low, &hi);
  1084.       overflow = overflow_sum_sign (hi, int2h, int1h);
  1085.       t = build_int_2 (low, hi);
  1086.       break;
  1087.  
  1088.     case MULT_EXPR:
  1089.       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
  1090.       t = build_int_2 (low, hi);
  1091.       break;
  1092.  
  1093.     case TRUNC_DIV_EXPR:
  1094.     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
  1095.     case EXACT_DIV_EXPR:
  1096.       /* This is a shortcut for a common special case.
  1097.          It reduces the number of tree nodes generated
  1098.          and saves time.  */
  1099.       if (int2h == 0 && int2l > 0
  1100.           && TREE_TYPE (arg1) == sizetype
  1101.           && int1h == 0 && int1l >= 0)
  1102.         {
  1103.           if (code == CEIL_DIV_EXPR)
  1104.         int1l += int2l-1;
  1105.           return size_int (int1l / int2l);
  1106.         }
  1107.     case ROUND_DIV_EXPR: 
  1108.       if (int2h == 0 && int2l == 1)
  1109.         {
  1110.           t = build_int_2 (int1l, int1h);
  1111.           break;
  1112.         }
  1113.       if (int1l == int2l && int1h == int2h)
  1114.         {
  1115.           if ((int1l | int1h) == 0)
  1116.         abort ();
  1117.           t = build_int_2 (1, 0);
  1118.           break;
  1119.         }
  1120.       overflow = div_and_round_double (code, uns,
  1121.                        int1l, int1h, int2l, int2h,
  1122.                        &low, &hi, &garbagel, &garbageh);
  1123.       t = build_int_2 (low, hi);
  1124.       break;
  1125.  
  1126.     case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
  1127.     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
  1128.       overflow = div_and_round_double (code, uns,
  1129.                        int1l, int1h, int2l, int2h,
  1130.                        &garbagel, &garbageh, &low, &hi);
  1131.       t = build_int_2 (low, hi);
  1132.       break;
  1133.  
  1134.     case MIN_EXPR:
  1135.     case MAX_EXPR:
  1136.       if (uns)
  1137.         {
  1138.           low = (((unsigned HOST_WIDE_INT) int1h
  1139.               < (unsigned HOST_WIDE_INT) int2h)
  1140.              || (((unsigned HOST_WIDE_INT) int1h
  1141.               == (unsigned HOST_WIDE_INT) int2h)
  1142.              && ((unsigned HOST_WIDE_INT) int1l
  1143.                  < (unsigned HOST_WIDE_INT) int2l)));
  1144.         }
  1145.       else
  1146.         {
  1147.           low = ((int1h < int2h)
  1148.              || ((int1h == int2h)
  1149.              && ((unsigned HOST_WIDE_INT) int1l
  1150.                  < (unsigned HOST_WIDE_INT) int2l)));
  1151.         }
  1152.       if (low == (code == MIN_EXPR))
  1153.         t = build_int_2 (int1l, int1h);
  1154.       else
  1155.         t = build_int_2 (int2l, int2h);
  1156.       break;
  1157.  
  1158.     default:
  1159.       abort ();
  1160.     }
  1161.     got_it:
  1162.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1163.       TREE_OVERFLOW (t)
  1164.     = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
  1165.        | TREE_OVERFLOW (arg1)
  1166.        | TREE_OVERFLOW (arg2));
  1167.       TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
  1168.                     | TREE_CONSTANT_OVERFLOW (arg1)
  1169.                     | TREE_CONSTANT_OVERFLOW (arg2));
  1170.       return t;
  1171.     }
  1172. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1173.   if (TREE_CODE (arg1) == REAL_CST)
  1174.     {
  1175.       REAL_VALUE_TYPE d1;
  1176.       REAL_VALUE_TYPE d2;
  1177.       int overflow = 0;
  1178.       REAL_VALUE_TYPE value;
  1179.       tree t;
  1180.  
  1181.       d1 = TREE_REAL_CST (arg1);
  1182.       d2 = TREE_REAL_CST (arg2);
  1183.  
  1184.       /* If either operand is a NaN, just return it.  Otherwise, set up
  1185.      for floating-point trap; we return an overflow.  */
  1186.       if (REAL_VALUE_ISNAN (d1))
  1187.     return arg1;
  1188.       else if (REAL_VALUE_ISNAN (d2))
  1189.     return arg2;
  1190.       else if (setjmp (float_error))
  1191.     {
  1192.       t = copy_node (arg1);
  1193.       overflow = 1;
  1194.       goto got_float;
  1195.     }
  1196.  
  1197.       set_float_handler (float_error);
  1198.  
  1199. #ifdef REAL_ARITHMETIC
  1200.       REAL_ARITHMETIC (value, code, d1, d2);
  1201. #else
  1202.       switch (code)
  1203.     {
  1204.     case PLUS_EXPR:
  1205.       value = d1 + d2;
  1206.       break;
  1207.  
  1208.     case MINUS_EXPR:
  1209.       value = d1 - d2;
  1210.       break;
  1211.  
  1212.     case MULT_EXPR:
  1213.       value = d1 * d2;
  1214.       break;
  1215.  
  1216.     case RDIV_EXPR:
  1217. #ifndef REAL_INFINITY
  1218.       if (d2 == 0)
  1219.         abort ();
  1220. #endif
  1221.  
  1222.       value = d1 / d2;
  1223.       break;
  1224.  
  1225.     case MIN_EXPR:
  1226.       value = MIN (d1, d2);
  1227.       break;
  1228.  
  1229.     case MAX_EXPR:
  1230.       value = MAX (d1, d2);
  1231.       break;
  1232.  
  1233.     default:
  1234.       abort ();
  1235.     }
  1236. #endif /* no REAL_ARITHMETIC */
  1237.       t = build_real (TREE_TYPE (arg1),
  1238.               real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
  1239.     got_float:
  1240.       set_float_handler (NULL_PTR);
  1241.  
  1242.       TREE_OVERFLOW (t)
  1243.     = (force_fit_type (t, overflow)
  1244.        | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
  1245.       TREE_CONSTANT_OVERFLOW (t)
  1246.     = TREE_OVERFLOW (t)
  1247.       | TREE_CONSTANT_OVERFLOW (arg1)
  1248.       | TREE_CONSTANT_OVERFLOW (arg2);
  1249.       return t;
  1250.     }
  1251. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1252.   if (TREE_CODE (arg1) == COMPLEX_CST)
  1253.     {
  1254.       register tree r1 = TREE_REALPART (arg1);
  1255.       register tree i1 = TREE_IMAGPART (arg1);
  1256.       register tree r2 = TREE_REALPART (arg2);
  1257.       register tree i2 = TREE_IMAGPART (arg2);
  1258.       register tree t;
  1259.  
  1260.       switch (code)
  1261.     {
  1262.     case PLUS_EXPR:
  1263.       t = build_complex (const_binop (PLUS_EXPR, r1, r2, notrunc),
  1264.                  const_binop (PLUS_EXPR, i1, i2, notrunc));
  1265.       break;
  1266.  
  1267.     case MINUS_EXPR:
  1268.       t = build_complex (const_binop (MINUS_EXPR, r1, r2, notrunc),
  1269.                  const_binop (MINUS_EXPR, i1, i2, notrunc));
  1270.       break;
  1271.  
  1272.     case MULT_EXPR:
  1273.       t = build_complex (const_binop (MINUS_EXPR,
  1274.                       const_binop (MULT_EXPR,
  1275.                                r1, r2, notrunc),
  1276.                       const_binop (MULT_EXPR,
  1277.                                i1, i2, notrunc),
  1278.                       notrunc),
  1279.                  const_binop (PLUS_EXPR,
  1280.                       const_binop (MULT_EXPR,
  1281.                                r1, i2, notrunc),
  1282.                       const_binop (MULT_EXPR,
  1283.                                i1, r2, notrunc),
  1284.                       notrunc));
  1285.       break;
  1286.  
  1287.     case RDIV_EXPR:
  1288.       {
  1289.         register tree magsquared
  1290.           = const_binop (PLUS_EXPR,
  1291.                  const_binop (MULT_EXPR, r2, r2, notrunc),
  1292.                  const_binop (MULT_EXPR, i2, i2, notrunc),
  1293.                  notrunc);
  1294.  
  1295.         t = build_complex
  1296.           (const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
  1297.                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
  1298.                 const_binop (PLUS_EXPR,
  1299.                      const_binop (MULT_EXPR, r1, r2,
  1300.                               notrunc),
  1301.                      const_binop (MULT_EXPR, i1, i2,
  1302.                               notrunc),
  1303.                      notrunc),
  1304.                 magsquared, notrunc),
  1305.            const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
  1306.                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
  1307.                 const_binop (MINUS_EXPR,
  1308.                      const_binop (MULT_EXPR, i1, r2,
  1309.                               notrunc),
  1310.                      const_binop (MULT_EXPR, r1, i2,
  1311.                               notrunc),
  1312.                      notrunc),
  1313.                 magsquared, notrunc));
  1314.       }
  1315.       break;
  1316.  
  1317.     default:
  1318.       abort ();
  1319.     }
  1320.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1321.       return t;
  1322.     }
  1323.   return 0;
  1324. }
  1325.  
  1326. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  1327.  
  1328. tree
  1329. size_int (number)
  1330.      unsigned int number;
  1331. {
  1332.   register tree t;
  1333.   /* Type-size nodes already made for small sizes.  */
  1334.   static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
  1335.  
  1336.   if (number < 2*HOST_BITS_PER_WIDE_INT + 1
  1337.       && size_table[number] != 0)
  1338.     return size_table[number];
  1339.   if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
  1340.     {
  1341.       push_obstacks_nochange ();
  1342.       /* Make this a permanent node.  */
  1343.       end_temporary_allocation ();
  1344.       t = build_int_2 (number, 0);
  1345.       TREE_TYPE (t) = sizetype;
  1346.       size_table[number] = t;
  1347.       pop_obstacks ();
  1348.     }
  1349.   else
  1350.     {
  1351.       t = build_int_2 (number, 0);
  1352.       TREE_TYPE (t) = sizetype;
  1353.     }
  1354.   return t;
  1355. }
  1356.  
  1357. /* Combine operands OP1 and OP2 with arithmetic operation CODE.
  1358.    CODE is a tree code.  Data type is taken from `sizetype',
  1359.    If the operands are constant, so is the result.  */
  1360.  
  1361. tree
  1362. size_binop (code, arg0, arg1)
  1363.      enum tree_code code;
  1364.      tree arg0, arg1;
  1365. {
  1366.   /* Handle the special case of two integer constants faster.  */
  1367.   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  1368.     {
  1369.       /* And some specific cases even faster than that.  */
  1370.       if (code == PLUS_EXPR
  1371.       && TREE_INT_CST_LOW (arg0) == 0
  1372.       && TREE_INT_CST_HIGH (arg0) == 0)
  1373.     return arg1;
  1374.       if (code == MINUS_EXPR
  1375.       && TREE_INT_CST_LOW (arg1) == 0
  1376.       && TREE_INT_CST_HIGH (arg1) == 0)
  1377.     return arg0;
  1378.       if (code == MULT_EXPR
  1379.       && TREE_INT_CST_LOW (arg0) == 1
  1380.       && TREE_INT_CST_HIGH (arg0) == 0)
  1381.     return arg1;
  1382.       /* Handle general case of two integer constants.  */
  1383.       return const_binop (code, arg0, arg1, 1);
  1384.     }
  1385.  
  1386.   if (arg0 == error_mark_node || arg1 == error_mark_node)
  1387.     return error_mark_node;
  1388.  
  1389.   return fold (build (code, sizetype, arg0, arg1));
  1390. }
  1391.  
  1392. /* Given T, a tree representing type conversion of ARG1, a constant,
  1393.    return a constant tree representing the result of conversion.  */
  1394.  
  1395. static tree
  1396. fold_convert (t, arg1)
  1397.      register tree t;
  1398.      register tree arg1;
  1399. {
  1400.   register tree type = TREE_TYPE (t);
  1401.   int overflow = 0;
  1402.  
  1403.   if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
  1404.     {
  1405.       if (TREE_CODE (arg1) == INTEGER_CST)
  1406.     {
  1407.       /* If we would build a constant wider than GCC supports,
  1408.          leave the conversion unfolded.  */
  1409.       if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
  1410.         return t;
  1411.  
  1412.       /* Given an integer constant, make new constant with new type,
  1413.          appropriately sign-extended or truncated.  */
  1414.       t = build_int_2 (TREE_INT_CST_LOW (arg1),
  1415.                TREE_INT_CST_HIGH (arg1));
  1416.       TREE_TYPE (t) = type;
  1417.       /* Indicate an overflow if (1) ARG1 already overflowed,
  1418.          or (2) force_fit_type indicates an overflow.
  1419.          Tell force_fit_type that an overflow has already occurred
  1420.          if ARG1 is a too-large unsigned value and T is signed.  */
  1421.       TREE_OVERFLOW (t)
  1422.         = (TREE_OVERFLOW (arg1)
  1423.            | force_fit_type (t,
  1424.                  (TREE_INT_CST_HIGH (arg1) < 0
  1425.                   & (TREE_UNSIGNED (type)
  1426.                      < TREE_UNSIGNED (TREE_TYPE (arg1))))));
  1427.       TREE_CONSTANT_OVERFLOW (t)
  1428.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1429.     }
  1430. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1431.       else if (TREE_CODE (arg1) == REAL_CST)
  1432.     {
  1433.       /* Don't initialize these, use assignments.
  1434.          Initialized local aggregates don't work on old compilers.  */
  1435.       REAL_VALUE_TYPE x;
  1436.       REAL_VALUE_TYPE l;
  1437.       REAL_VALUE_TYPE u;
  1438.  
  1439.       x = TREE_REAL_CST (arg1);
  1440.       l = real_value_from_int_cst (TYPE_MIN_VALUE (type));
  1441.       u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
  1442.       /* See if X will be in range after truncation towards 0.
  1443.          To compensate for truncation, move the bounds away from 0,
  1444.          but reject if X exactly equals the adjusted bounds.  */
  1445. #ifdef REAL_ARITHMETIC
  1446.       REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
  1447.       REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
  1448. #else
  1449.       l--;
  1450.       u++;
  1451. #endif
  1452.       /* If X is a NaN, use zero instead and show we have an overflow.
  1453.          Otherwise, range check.  */
  1454.       if (REAL_VALUE_ISNAN (x))
  1455.         overflow = 1, x = dconst0;
  1456.       else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
  1457.         overflow = 1;
  1458.  
  1459. #ifndef REAL_ARITHMETIC
  1460.       {
  1461.         HOST_WIDE_INT low, high;
  1462.         HOST_WIDE_INT half_word
  1463.           = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
  1464.  
  1465.         if (x < 0)
  1466.           x = -x;
  1467.  
  1468.         high = (HOST_WIDE_INT) (x / half_word / half_word);
  1469.         x -= (REAL_VALUE_TYPE) high * half_word * half_word;
  1470.         if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
  1471.           {
  1472.         low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
  1473.         low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
  1474.           }
  1475.         else
  1476.           low = (HOST_WIDE_INT) x;
  1477.         if (TREE_REAL_CST (arg1) < 0)
  1478.           neg_double (low, high, &low, &high);
  1479.         t = build_int_2 (low, high);
  1480.       }
  1481. #else
  1482.       {
  1483.         HOST_WIDE_INT low, high;
  1484.         REAL_VALUE_TO_INT (&low, &high, x);
  1485.         t = build_int_2 (low, high);
  1486.       }
  1487. #endif
  1488.       TREE_TYPE (t) = type;
  1489.       TREE_OVERFLOW (t)
  1490.         = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
  1491.       TREE_CONSTANT_OVERFLOW (t)
  1492.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1493.     }
  1494. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1495.       TREE_TYPE (t) = type;
  1496.     }
  1497.   else if (TREE_CODE (type) == REAL_TYPE)
  1498.     {
  1499. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1500.       if (TREE_CODE (arg1) == INTEGER_CST)
  1501.     return build_real_from_int_cst (type, arg1);
  1502. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1503.       if (TREE_CODE (arg1) == REAL_CST)
  1504.     {
  1505.       if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
  1506.         return arg1;
  1507.       else if (setjmp (float_error))
  1508.         {
  1509.           overflow = 1;
  1510.           t = copy_node (arg1);
  1511.           goto got_it;
  1512.         }
  1513.       set_float_handler (float_error);
  1514.  
  1515.       t = build_real (type, real_value_truncate (TYPE_MODE (type),
  1516.                              TREE_REAL_CST (arg1)));
  1517.       set_float_handler (NULL_PTR);
  1518.  
  1519.     got_it:
  1520.       TREE_OVERFLOW (t)
  1521.         = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
  1522.       TREE_CONSTANT_OVERFLOW (t)
  1523.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1524.       return t;
  1525.     }
  1526.     }
  1527.   TREE_CONSTANT (t) = 1;
  1528.   return t;
  1529. }
  1530.  
  1531. /* Return an expr equal to X but certainly not valid as an lvalue.
  1532.    Also make sure it is not valid as an null pointer constant.  */
  1533.  
  1534. tree
  1535. non_lvalue (x)
  1536.      tree x;
  1537. {
  1538.   tree result;
  1539.  
  1540.   /* These things are certainly not lvalues.  */
  1541.   if (TREE_CODE (x) == NON_LVALUE_EXPR
  1542.       || TREE_CODE (x) == INTEGER_CST
  1543.       || TREE_CODE (x) == REAL_CST
  1544.       || TREE_CODE (x) == STRING_CST
  1545.       || TREE_CODE (x) == ADDR_EXPR)
  1546.     {
  1547.       if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
  1548.     {
  1549.       /* Use NOP_EXPR instead of NON_LVALUE_EXPR
  1550.          so convert_for_assignment won't strip it.
  1551.          This is so this 0 won't be treated as a null pointer constant.  */
  1552.       result = build1 (NOP_EXPR, TREE_TYPE (x), x);
  1553.       TREE_CONSTANT (result) = TREE_CONSTANT (x);
  1554.       return result;
  1555.     }
  1556.       return x;
  1557.     }
  1558.  
  1559.   result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
  1560.   TREE_CONSTANT (result) = TREE_CONSTANT (x);
  1561.   return result;
  1562. }
  1563.  
  1564. /* When pedantic, return an expr equal to X but certainly not valid as a
  1565.    pedantic lvalue.  Otherwise, return X.  */
  1566.  
  1567. tree
  1568. pedantic_non_lvalue (x)
  1569.      tree x;
  1570. {
  1571.   if (pedantic)
  1572.     return non_lvalue (x);
  1573.   else
  1574.     return x;
  1575. }
  1576.  
  1577. /* Given a tree comparison code, return the code that is the logical inverse
  1578.    of the given code.  It is not safe to do this for floating-point
  1579.    comparisons, except for NE_EXPR and EQ_EXPR.  */
  1580.  
  1581. static enum tree_code
  1582. invert_tree_comparison (code)
  1583.      enum tree_code code;
  1584. {
  1585.   switch (code)
  1586.     {
  1587.     case EQ_EXPR:
  1588.       return NE_EXPR;
  1589.     case NE_EXPR:
  1590.       return EQ_EXPR;
  1591.     case GT_EXPR:
  1592.       return LE_EXPR;
  1593.     case GE_EXPR:
  1594.       return LT_EXPR;
  1595.     case LT_EXPR:
  1596.       return GE_EXPR;
  1597.     case LE_EXPR:
  1598.       return GT_EXPR;
  1599.     default:
  1600.       abort ();
  1601.     }
  1602. }
  1603.  
  1604. /* Similar, but return the comparison that results if the operands are
  1605.    swapped.  This is safe for floating-point.  */
  1606.  
  1607. static enum tree_code
  1608. swap_tree_comparison (code)
  1609.      enum tree_code code;
  1610. {
  1611.   switch (code)
  1612.     {
  1613.     case EQ_EXPR:
  1614.     case NE_EXPR:
  1615.       return code;
  1616.     case GT_EXPR:
  1617.       return LT_EXPR;
  1618.     case GE_EXPR:
  1619.       return LE_EXPR;
  1620.     case LT_EXPR:
  1621.       return GT_EXPR;
  1622.     case LE_EXPR:
  1623.       return GE_EXPR;
  1624.     default:
  1625.       abort ();
  1626.     }
  1627. }
  1628.  
  1629. /* Return nonzero if CODE is a tree code that represents a truth value.  */
  1630.  
  1631. static int
  1632. truth_value_p (code)
  1633.      enum tree_code code;
  1634. {
  1635.   return (TREE_CODE_CLASS (code) == '<'
  1636.       || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
  1637.       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
  1638.       || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
  1639. }
  1640.  
  1641. /* Return nonzero if two operands are necessarily equal.
  1642.    If ONLY_CONST is non-zero, only return non-zero for constants.
  1643.    This function tests whether the operands are indistinguishable;
  1644.    it does not test whether they are equal using C's == operation.
  1645.    The distinction is important for IEEE floating point, because
  1646.    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
  1647.    (2) two NaNs may be indistinguishable, but NaN!=NaN.  */
  1648.  
  1649. int
  1650. operand_equal_p (arg0, arg1, only_const)
  1651.      tree arg0, arg1;
  1652.      int only_const;
  1653. {
  1654.   /* If both types don't have the same signedness, then we can't consider
  1655.      them equal.  We must check this before the STRIP_NOPS calls
  1656.      because they may change the signedness of the arguments.  */
  1657.   if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
  1658.     return 0;
  1659.  
  1660.   STRIP_NOPS (arg0);
  1661.   STRIP_NOPS (arg1);
  1662.  
  1663.   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
  1664.      We don't care about side effects in that case because the SAVE_EXPR
  1665.      takes care of that for us.  */
  1666.   if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
  1667.     return ! only_const;
  1668.  
  1669.   if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
  1670.     return 0;
  1671.  
  1672.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1673.       && TREE_CODE (arg0) == ADDR_EXPR
  1674.       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
  1675.     return 1;
  1676.  
  1677.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1678.       && TREE_CODE (arg0) == INTEGER_CST
  1679.       && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  1680.       && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
  1681.     return 1;
  1682.  
  1683.   /* Detect when real constants are equal.  */
  1684.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1685.       && TREE_CODE (arg0) == REAL_CST)
  1686.     return !bcmp ((char *) &TREE_REAL_CST (arg0),
  1687.           (char *) &TREE_REAL_CST (arg1),
  1688.           sizeof (REAL_VALUE_TYPE));
  1689.  
  1690.   if (only_const)
  1691.     return 0;
  1692.  
  1693.   if (arg0 == arg1)
  1694.     return 1;
  1695.  
  1696.   if (TREE_CODE (arg0) != TREE_CODE (arg1))
  1697.     return 0;
  1698.   /* This is needed for conversions and for COMPONENT_REF.
  1699.      Might as well play it safe and always test this.  */
  1700.   if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
  1701.     return 0;
  1702.  
  1703.   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
  1704.     {
  1705.     case '1':
  1706.       /* Two conversions are equal only if signedness and modes match.  */
  1707.       if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
  1708.       && (TREE_UNSIGNED (TREE_TYPE (arg0))
  1709.           != TREE_UNSIGNED (TREE_TYPE (arg1))))
  1710.     return 0;
  1711.  
  1712.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1713.                   TREE_OPERAND (arg1, 0), 0);
  1714.  
  1715.     case '<':
  1716.     case '2':
  1717.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1718.                    TREE_OPERAND (arg1, 0), 0)
  1719.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1720.                   TREE_OPERAND (arg1, 1), 0));
  1721.  
  1722.     case 'r':
  1723.       switch (TREE_CODE (arg0))
  1724.     {
  1725.     case INDIRECT_REF:
  1726.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1727.                   TREE_OPERAND (arg1, 0), 0);
  1728.  
  1729.     case COMPONENT_REF:
  1730.     case ARRAY_REF:
  1731.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1732.                    TREE_OPERAND (arg1, 0), 0)
  1733.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1734.                       TREE_OPERAND (arg1, 1), 0));
  1735.  
  1736.     case BIT_FIELD_REF:
  1737.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1738.                    TREE_OPERAND (arg1, 0), 0)
  1739.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1740.                       TREE_OPERAND (arg1, 1), 0)
  1741.           && operand_equal_p (TREE_OPERAND (arg0, 2),
  1742.                       TREE_OPERAND (arg1, 2), 0));
  1743.     }
  1744.       break;
  1745.     }
  1746.  
  1747.   return 0;
  1748. }
  1749.  
  1750. /* Similar to operand_equal_p, but see if ARG0 might have been made by
  1751.    shorten_compare from ARG1 when ARG1 was being compared with OTHER. 
  1752.  
  1753.    When in doubt, return 0.  */
  1754.  
  1755. static int 
  1756. operand_equal_for_comparison_p (arg0, arg1, other)
  1757.      tree arg0, arg1;
  1758.      tree other;
  1759. {
  1760.   int unsignedp1, unsignedpo;
  1761.   tree primarg1, primother;
  1762.   unsigned correct_width;
  1763.  
  1764.   if (operand_equal_p (arg0, arg1, 0))
  1765.     return 1;
  1766.  
  1767.   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  1768.     return 0;
  1769.  
  1770.   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
  1771.      actual comparison operand, ARG0.
  1772.  
  1773.      First throw away any conversions to wider types
  1774.      already present in the operands.  */
  1775.  
  1776.   primarg1 = get_narrower (arg1, &unsignedp1);
  1777.   primother = get_narrower (other, &unsignedpo);
  1778.  
  1779.   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
  1780.   if (unsignedp1 == unsignedpo
  1781.       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
  1782.       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
  1783.     {
  1784.       tree type = TREE_TYPE (arg0);
  1785.  
  1786.       /* Make sure shorter operand is extended the right way
  1787.      to match the longer operand.  */
  1788.       primarg1 = convert (signed_or_unsigned_type (unsignedp1,
  1789.                           TREE_TYPE (primarg1)),
  1790.              primarg1);
  1791.  
  1792.       if (operand_equal_p (arg0, convert (type, primarg1), 0))
  1793.     return 1;
  1794.     }
  1795.  
  1796.   return 0;
  1797. }
  1798.  
  1799. /* See if ARG is an expression that is either a comparison or is performing
  1800.    arithmetic on comparisons.  The comparisons must only be comparing
  1801.    two different values, which will be stored in *CVAL1 and *CVAL2; if
  1802.    they are non-zero it means that some operands have already been found.
  1803.    No variables may be used anywhere else in the expression except in the
  1804.    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
  1805.    the expression and save_expr needs to be called with CVAL1 and CVAL2.
  1806.  
  1807.    If this is true, return 1.  Otherwise, return zero.  */
  1808.  
  1809. static int
  1810. twoval_comparison_p (arg, cval1, cval2, save_p)
  1811.      tree arg;
  1812.      tree *cval1, *cval2;
  1813.      int *save_p;
  1814. {
  1815.   enum tree_code code = TREE_CODE (arg);
  1816.   char class = TREE_CODE_CLASS (code);
  1817.  
  1818.   /* We can handle some of the 'e' cases here.  */
  1819.   if (class == 'e' && code == TRUTH_NOT_EXPR)
  1820.     class = '1';
  1821.   else if (class == 'e'
  1822.        && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
  1823.            || code == COMPOUND_EXPR))
  1824.     class = '2';
  1825.  
  1826.   /* ??? Disable this since the SAVE_EXPR might already be in use outside
  1827.      the expression.  There may be no way to make this work, but it needs
  1828.      to be looked at again for 2.6.  */
  1829. #if 0
  1830.   else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
  1831.     {
  1832.       /* If we've already found a CVAL1 or CVAL2, this expression is
  1833.      two complex to handle.  */
  1834.       if (*cval1 || *cval2)
  1835.     return 0;
  1836.  
  1837.       class = '1';
  1838.       *save_p = 1;
  1839.     }
  1840. #endif
  1841.  
  1842.   switch (class)
  1843.     {
  1844.     case '1':
  1845.       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
  1846.  
  1847.     case '2':
  1848.       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
  1849.           && twoval_comparison_p (TREE_OPERAND (arg, 1),
  1850.                       cval1, cval2, save_p));
  1851.  
  1852.     case 'c':
  1853.       return 1;
  1854.  
  1855.     case 'e':
  1856.       if (code == COND_EXPR)
  1857.     return (twoval_comparison_p (TREE_OPERAND (arg, 0),
  1858.                      cval1, cval2, save_p)
  1859.         && twoval_comparison_p (TREE_OPERAND (arg, 1),
  1860.                     cval1, cval2, save_p)
  1861.         && twoval_comparison_p (TREE_OPERAND (arg, 2),
  1862.                     cval1, cval2, save_p));
  1863.       return 0;
  1864.       
  1865.     case '<':
  1866.       /* First see if we can handle the first operand, then the second.  For
  1867.      the second operand, we know *CVAL1 can't be zero.  It must be that
  1868.      one side of the comparison is each of the values; test for the
  1869.      case where this isn't true by failing if the two operands
  1870.      are the same.  */
  1871.  
  1872.       if (operand_equal_p (TREE_OPERAND (arg, 0),
  1873.                TREE_OPERAND (arg, 1), 0))
  1874.     return 0;
  1875.  
  1876.       if (*cval1 == 0)
  1877.     *cval1 = TREE_OPERAND (arg, 0);
  1878.       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
  1879.     ;
  1880.       else if (*cval2 == 0)
  1881.     *cval2 = TREE_OPERAND (arg, 0);
  1882.       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
  1883.     ;
  1884.       else
  1885.     return 0;
  1886.  
  1887.       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
  1888.     ;
  1889.       else if (*cval2 == 0)
  1890.     *cval2 = TREE_OPERAND (arg, 1);
  1891.       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
  1892.     ;
  1893.       else
  1894.     return 0;
  1895.  
  1896.       return 1;
  1897.     }
  1898.  
  1899.   return 0;
  1900. }
  1901.  
  1902. /* ARG is a tree that is known to contain just arithmetic operations and
  1903.    comparisons.  Evaluate the operations in the tree substituting NEW0 for
  1904.    any occurrence of OLD0 as an operand of a comparison and likewise for
  1905.    NEW1 and OLD1.  */
  1906.  
  1907. static tree
  1908. eval_subst (arg, old0, new0, old1, new1)
  1909.      tree arg;
  1910.      tree old0, new0, old1, new1;
  1911. {
  1912.   tree type = TREE_TYPE (arg);
  1913.   enum tree_code code = TREE_CODE (arg);
  1914.   char class = TREE_CODE_CLASS (code);
  1915.  
  1916.   /* We can handle some of the 'e' cases here.  */
  1917.   if (class == 'e' && code == TRUTH_NOT_EXPR)
  1918.     class = '1';
  1919.   else if (class == 'e'
  1920.        && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
  1921.     class = '2';
  1922.  
  1923.   switch (class)
  1924.     {
  1925.     case '1':
  1926.       return fold (build1 (code, type,
  1927.                eval_subst (TREE_OPERAND (arg, 0),
  1928.                        old0, new0, old1, new1)));
  1929.  
  1930.     case '2':
  1931.       return fold (build (code, type,
  1932.               eval_subst (TREE_OPERAND (arg, 0),
  1933.                       old0, new0, old1, new1),
  1934.               eval_subst (TREE_OPERAND (arg, 1),
  1935.                       old0, new0, old1, new1)));
  1936.  
  1937.     case 'e':
  1938.       switch (code)
  1939.     {
  1940.     case SAVE_EXPR:
  1941.       return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
  1942.  
  1943.     case COMPOUND_EXPR:
  1944.       return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
  1945.  
  1946.     case COND_EXPR:
  1947.       return fold (build (code, type,
  1948.                   eval_subst (TREE_OPERAND (arg, 0),
  1949.                       old0, new0, old1, new1),
  1950.                   eval_subst (TREE_OPERAND (arg, 1),
  1951.                       old0, new0, old1, new1),
  1952.                   eval_subst (TREE_OPERAND (arg, 2),
  1953.                       old0, new0, old1, new1)));
  1954.     }
  1955.  
  1956.     case '<':
  1957.       {
  1958.     tree arg0 = TREE_OPERAND (arg, 0);
  1959.     tree arg1 = TREE_OPERAND (arg, 1);
  1960.  
  1961.     /* We need to check both for exact equality and tree equality.  The
  1962.        former will be true if the operand has a side-effect.  In that
  1963.        case, we know the operand occurred exactly once.  */
  1964.  
  1965.     if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
  1966.       arg0 = new0;
  1967.     else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
  1968.       arg0 = new1;
  1969.  
  1970.     if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
  1971.       arg1 = new0;
  1972.     else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
  1973.       arg1 = new1;
  1974.  
  1975.     return fold (build (code, type, arg0, arg1));
  1976.       }
  1977.     }
  1978.  
  1979.   return arg;
  1980. }
  1981.  
  1982. /* Return a tree for the case when the result of an expression is RESULT
  1983.    converted to TYPE and OMITTED was previously an operand of the expression
  1984.    but is now not needed (e.g., we folded OMITTED * 0).
  1985.  
  1986.    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
  1987.    the conversion of RESULT to TYPE.  */
  1988.  
  1989. static tree
  1990. omit_one_operand (type, result, omitted)
  1991.      tree type, result, omitted;
  1992. {
  1993.   tree t = convert (type, result);
  1994.  
  1995.   if (TREE_SIDE_EFFECTS (omitted))
  1996.     return build (COMPOUND_EXPR, type, omitted, t);
  1997.  
  1998.   return non_lvalue (t);
  1999. }
  2000.  
  2001. /* Return a simplified tree node for the truth-negation of ARG.  This
  2002.    never alters ARG itself.  We assume that ARG is an operation that
  2003.    returns a truth value (0 or 1).  */
  2004.  
  2005. tree
  2006. invert_truthvalue (arg)
  2007.      tree arg;
  2008. {
  2009.   tree type = TREE_TYPE (arg);
  2010.   enum tree_code code = TREE_CODE (arg);
  2011.  
  2012.   if (code == ERROR_MARK)
  2013.     return arg;
  2014.  
  2015.   /* If this is a comparison, we can simply invert it, except for
  2016.      floating-point non-equality comparisons, in which case we just
  2017.      enclose a TRUTH_NOT_EXPR around what we have.  */
  2018.  
  2019.   if (TREE_CODE_CLASS (code) == '<')
  2020.     {
  2021.       if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
  2022.       && code != NE_EXPR && code != EQ_EXPR)
  2023.     return build1 (TRUTH_NOT_EXPR, type, arg);
  2024.       else
  2025.     return build (invert_tree_comparison (code), type,
  2026.               TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
  2027.     }
  2028.  
  2029.   switch (code)
  2030.     {
  2031.     case INTEGER_CST:
  2032.       return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
  2033.                      && TREE_INT_CST_HIGH (arg) == 0, 0));
  2034.  
  2035.     case TRUTH_AND_EXPR:
  2036.       return build (TRUTH_OR_EXPR, type,
  2037.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2038.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2039.  
  2040.     case TRUTH_OR_EXPR:
  2041.       return build (TRUTH_AND_EXPR, type,
  2042.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2043.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2044.  
  2045.     case TRUTH_XOR_EXPR:
  2046.       /* Here we can invert either operand.  We invert the first operand
  2047.      unless the second operand is a TRUTH_NOT_EXPR in which case our
  2048.      result is the XOR of the first operand with the inside of the
  2049.      negation of the second operand.  */
  2050.  
  2051.       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
  2052.     return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
  2053.               TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
  2054.       else
  2055.     return build (TRUTH_XOR_EXPR, type,
  2056.               invert_truthvalue (TREE_OPERAND (arg, 0)),
  2057.               TREE_OPERAND (arg, 1));
  2058.  
  2059.     case TRUTH_ANDIF_EXPR:
  2060.       return build (TRUTH_ORIF_EXPR, type,
  2061.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2062.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2063.  
  2064.     case TRUTH_ORIF_EXPR:
  2065.       return build (TRUTH_ANDIF_EXPR, type,
  2066.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2067.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2068.  
  2069.     case TRUTH_NOT_EXPR:
  2070.       return TREE_OPERAND (arg, 0);
  2071.  
  2072.     case COND_EXPR:
  2073.       return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
  2074.             invert_truthvalue (TREE_OPERAND (arg, 1)),
  2075.             invert_truthvalue (TREE_OPERAND (arg, 2)));
  2076.  
  2077.     case COMPOUND_EXPR:
  2078.       return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
  2079.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2080.  
  2081.     case NON_LVALUE_EXPR:
  2082.       return invert_truthvalue (TREE_OPERAND (arg, 0));
  2083.  
  2084.     case NOP_EXPR:
  2085.     case CONVERT_EXPR:
  2086.     case FLOAT_EXPR:
  2087.       return build1 (TREE_CODE (arg), type,
  2088.              invert_truthvalue (TREE_OPERAND (arg, 0)));
  2089.  
  2090.     case BIT_AND_EXPR:
  2091.       if (!integer_onep (TREE_OPERAND (arg, 1)))
  2092.     break;
  2093.       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
  2094.  
  2095.     case SAVE_EXPR:
  2096.       return build1 (TRUTH_NOT_EXPR, type, arg);
  2097.     }
  2098.   if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
  2099.     abort ();
  2100.   return build1 (TRUTH_NOT_EXPR, type, arg);
  2101. }
  2102.  
  2103. /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
  2104.    operands are another bit-wise operation with a common input.  If so,
  2105.    distribute the bit operations to save an operation and possibly two if
  2106.    constants are involved.  For example, convert
  2107.        (A | B) & (A | C) into A | (B & C)
  2108.    Further simplification will occur if B and C are constants.
  2109.  
  2110.    If this optimization cannot be done, 0 will be returned.  */
  2111.  
  2112. static tree
  2113. distribute_bit_expr (code, type, arg0, arg1)
  2114.      enum tree_code code;
  2115.      tree type;
  2116.      tree arg0, arg1;
  2117. {
  2118.   tree common;
  2119.   tree left, right;
  2120.  
  2121.   if (TREE_CODE (arg0) != TREE_CODE (arg1)
  2122.       || TREE_CODE (arg0) == code
  2123.       || (TREE_CODE (arg0) != BIT_AND_EXPR
  2124.       && TREE_CODE (arg0) != BIT_IOR_EXPR))
  2125.     return 0;
  2126.  
  2127.   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
  2128.     {
  2129.       common = TREE_OPERAND (arg0, 0);
  2130.       left = TREE_OPERAND (arg0, 1);
  2131.       right = TREE_OPERAND (arg1, 1);
  2132.     }
  2133.   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
  2134.     {
  2135.       common = TREE_OPERAND (arg0, 0);
  2136.       left = TREE_OPERAND (arg0, 1);
  2137.       right = TREE_OPERAND (arg1, 0);
  2138.     }
  2139.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
  2140.     {
  2141.       common = TREE_OPERAND (arg0, 1);
  2142.       left = TREE_OPERAND (arg0, 0);
  2143.       right = TREE_OPERAND (arg1, 1);
  2144.     }
  2145.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
  2146.     {
  2147.       common = TREE_OPERAND (arg0, 1);
  2148.       left = TREE_OPERAND (arg0, 0);
  2149.       right = TREE_OPERAND (arg1, 0);
  2150.     }
  2151.   else
  2152.     return 0;
  2153.  
  2154.   return fold (build (TREE_CODE (arg0), type, common,
  2155.               fold (build (code, type, left, right))));
  2156. }
  2157.  
  2158. /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
  2159.    starting at BITPOS.  The field is unsigned if UNSIGNEDP is non-zero.  */
  2160.  
  2161. static tree
  2162. make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
  2163.      tree inner;
  2164.      tree type;
  2165.      int bitsize, bitpos;
  2166.      int unsignedp;
  2167. {
  2168.   tree result = build (BIT_FIELD_REF, type, inner,
  2169.                size_int (bitsize), size_int (bitpos));
  2170.  
  2171.   TREE_UNSIGNED (result) = unsignedp;
  2172.  
  2173.   return result;
  2174. }
  2175.  
  2176. /* Optimize a bit-field compare.
  2177.  
  2178.    There are two cases:  First is a compare against a constant and the
  2179.    second is a comparison of two items where the fields are at the same
  2180.    bit position relative to the start of a chunk (byte, halfword, word)
  2181.    large enough to contain it.  In these cases we can avoid the shift
  2182.    implicit in bitfield extractions.
  2183.  
  2184.    For constants, we emit a compare of the shifted constant with the
  2185.    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
  2186.    compared.  For two fields at the same position, we do the ANDs with the
  2187.    similar mask and compare the result of the ANDs.
  2188.  
  2189.    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
  2190.    COMPARE_TYPE is the type of the comparison, and LHS and RHS
  2191.    are the left and right operands of the comparison, respectively.
  2192.  
  2193.    If the optimization described above can be done, we return the resulting
  2194.    tree.  Otherwise we return zero.  */
  2195.  
  2196. static tree
  2197. optimize_bit_field_compare (code, compare_type, lhs, rhs)
  2198.      enum tree_code code;
  2199.      tree compare_type;
  2200.      tree lhs, rhs;
  2201. {
  2202.   int lbitpos, lbitsize, rbitpos, rbitsize;
  2203.   int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
  2204.   tree type = TREE_TYPE (lhs);
  2205.   tree signed_type, unsigned_type;
  2206.   int const_p = TREE_CODE (rhs) == INTEGER_CST;
  2207.   enum machine_mode lmode, rmode, lnmode, rnmode;
  2208.   int lunsignedp, runsignedp;
  2209.   int lvolatilep = 0, rvolatilep = 0;
  2210.   tree linner, rinner;
  2211.   tree mask;
  2212.   tree offset;
  2213.  
  2214.   /* Get all the information about the extractions being done.  If the bit size
  2215.      if the same as the size of the underlying object, we aren't doing an
  2216.      extraction at all and so can do nothing.  */
  2217.   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
  2218.                 &lunsignedp, &lvolatilep);
  2219.   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
  2220.       || offset != 0)
  2221.     return 0;
  2222.  
  2223.  if (!const_p)
  2224.    {
  2225.      /* If this is not a constant, we can only do something if bit positions,
  2226.     sizes, and signedness are the same.   */
  2227.      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
  2228.                    &rmode, &runsignedp, &rvolatilep);
  2229.  
  2230.      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
  2231.      || lunsignedp != runsignedp || offset != 0)
  2232.        return 0;
  2233.    }
  2234.  
  2235.   /* See if we can find a mode to refer to this field.  We should be able to,
  2236.      but fail if we can't.  */
  2237.   lnmode = get_best_mode (lbitsize, lbitpos,
  2238.               TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
  2239.               lvolatilep);
  2240.   if (lnmode == VOIDmode)
  2241.     return 0;
  2242.  
  2243.   /* Set signed and unsigned types of the precision of this mode for the
  2244.      shifts below.  */
  2245.   signed_type = type_for_mode (lnmode, 0);
  2246.   unsigned_type = type_for_mode (lnmode, 1);
  2247.  
  2248.   if (! const_p)
  2249.     {
  2250.       rnmode = get_best_mode (rbitsize, rbitpos, 
  2251.                   TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
  2252.                   rvolatilep);
  2253.       if (rnmode == VOIDmode)
  2254.     return 0;
  2255.     }
  2256.     
  2257.   /* Compute the bit position and size for the new reference and our offset
  2258.      within it. If the new reference is the same size as the original, we
  2259.      won't optimize anything, so return zero.  */
  2260.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  2261.   lnbitpos = lbitpos & ~ (lnbitsize - 1);
  2262.   lbitpos -= lnbitpos;
  2263.   if (lnbitsize == lbitsize)
  2264.     return 0;
  2265.  
  2266.   if (! const_p)
  2267.     {
  2268.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  2269.       rnbitpos = rbitpos & ~ (rnbitsize - 1);
  2270.       rbitpos -= rnbitpos;
  2271.       if (rnbitsize == rbitsize)
  2272.     return 0;
  2273.     }
  2274.  
  2275. #if BYTES_BIG_ENDIAN
  2276.   lbitpos = lnbitsize - lbitsize - lbitpos;
  2277. #endif
  2278.  
  2279.   /* Make the mask to be used against the extracted field.  */
  2280.   mask = build_int_2 (~0, ~0);
  2281.   TREE_TYPE (mask) = unsigned_type;
  2282.   force_fit_type (mask, 0);
  2283.   mask = convert (unsigned_type, mask);
  2284.   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
  2285.   mask = const_binop (RSHIFT_EXPR, mask,
  2286.               size_int (lnbitsize - lbitsize - lbitpos), 0);
  2287.  
  2288.   if (! const_p)
  2289.     /* If not comparing with constant, just rework the comparison
  2290.        and return.  */
  2291.     return build (code, compare_type,
  2292.           build (BIT_AND_EXPR, unsigned_type,
  2293.              make_bit_field_ref (linner, unsigned_type,
  2294.                          lnbitsize, lnbitpos, 1),
  2295.              mask),
  2296.           build (BIT_AND_EXPR, unsigned_type,
  2297.              make_bit_field_ref (rinner, unsigned_type,
  2298.                          rnbitsize, rnbitpos, 1),
  2299.              mask));
  2300.  
  2301.   /* Otherwise, we are handling the constant case. See if the constant is too
  2302.      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
  2303.      this not only for its own sake, but to avoid having to test for this
  2304.      error case below.  If we didn't, we might generate wrong code.
  2305.  
  2306.      For unsigned fields, the constant shifted right by the field length should
  2307.      be all zero.  For signed fields, the high-order bits should agree with 
  2308.      the sign bit.  */
  2309.  
  2310.   if (lunsignedp)
  2311.     {
  2312.       if (! integer_zerop (const_binop (RSHIFT_EXPR,
  2313.                     convert (unsigned_type, rhs),
  2314.                     size_int (lbitsize), 0)))
  2315.     {
  2316.       warning ("comparison is always %s due to width of bitfield",
  2317.            code == NE_EXPR ? "one" : "zero");
  2318.       return convert (compare_type,
  2319.               (code == NE_EXPR
  2320.                ? integer_one_node : integer_zero_node));
  2321.     }
  2322.     }
  2323.   else
  2324.     {
  2325.       tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
  2326.                   size_int (lbitsize - 1), 0);
  2327.       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
  2328.     {
  2329.       warning ("comparison is always %s due to width of bitfield",
  2330.            code == NE_EXPR ? "one" : "zero");
  2331.       return convert (compare_type,
  2332.               (code == NE_EXPR
  2333.                ? integer_one_node : integer_zero_node));
  2334.     }
  2335.     }
  2336.  
  2337.   /* Single-bit compares should always be against zero.  */
  2338.   if (lbitsize == 1 && ! integer_zerop (rhs))
  2339.     {
  2340.       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
  2341.       rhs = convert (type, integer_zero_node);
  2342.     }
  2343.  
  2344.   /* Make a new bitfield reference, shift the constant over the
  2345.      appropriate number of bits and mask it with the computed mask
  2346.      (in case this was a signed field).  If we changed it, make a new one.  */
  2347.   lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
  2348.   if (lvolatilep)
  2349.     {
  2350.       TREE_SIDE_EFFECTS (lhs) = 1;
  2351.       TREE_THIS_VOLATILE (lhs) = 1;
  2352.     }
  2353.  
  2354.   rhs = fold (const_binop (BIT_AND_EXPR,
  2355.                const_binop (LSHIFT_EXPR,
  2356.                     convert (unsigned_type, rhs),
  2357.                     size_int (lbitpos), 0),
  2358.                mask, 0));
  2359.  
  2360.   return build (code, compare_type,
  2361.         build (BIT_AND_EXPR, unsigned_type, lhs, mask),
  2362.         rhs);
  2363. }
  2364.  
  2365. /* Subroutine for fold_truthop: decode a field reference.
  2366.  
  2367.    If EXP is a comparison reference, we return the innermost reference.
  2368.  
  2369.    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
  2370.    set to the starting bit number.
  2371.  
  2372.    If the innermost field can be completely contained in a mode-sized
  2373.    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
  2374.  
  2375.    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
  2376.    otherwise it is not changed.
  2377.  
  2378.    *PUNSIGNEDP is set to the signedness of the field.
  2379.  
  2380.    *PMASK is set to the mask used.  This is either contained in a
  2381.    BIT_AND_EXPR or derived from the width of the field.
  2382.  
  2383.    Return 0 if this is not a component reference or is one that we can't
  2384.    do anything with.  */
  2385.  
  2386. static tree
  2387. decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
  2388.             pvolatilep, pmask)
  2389.      tree exp;
  2390.      int *pbitsize, *pbitpos;
  2391.      enum machine_mode *pmode;
  2392.      int *punsignedp, *pvolatilep;
  2393.      tree *pmask;
  2394. {
  2395.   tree and_mask = 0;
  2396.   tree mask, inner, offset;
  2397.   tree unsigned_type;
  2398.   int precision;
  2399.  
  2400.   /* All the optimizations using this function assume integer fields.  
  2401.      There are problems with FP fields since the type_for_size call
  2402.      below can fail for, e.g., XFmode.  */
  2403.   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
  2404.     return 0;
  2405.  
  2406.   STRIP_NOPS (exp);
  2407.  
  2408.   if (TREE_CODE (exp) == BIT_AND_EXPR)
  2409.     {
  2410.       and_mask = TREE_OPERAND (exp, 1);
  2411.       exp = TREE_OPERAND (exp, 0);
  2412.       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
  2413.       if (TREE_CODE (and_mask) != INTEGER_CST)
  2414.     return 0;
  2415.     }
  2416.  
  2417.   if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
  2418.       && TREE_CODE (exp) != BIT_FIELD_REF)
  2419.     return 0;
  2420.  
  2421.   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
  2422.                    punsignedp, pvolatilep);
  2423.   if (inner == exp || *pbitsize < 0 || offset != 0)
  2424.     return 0;
  2425.   
  2426.   /* Compute the mask to access the bitfield.  */
  2427.   unsigned_type = type_for_size (*pbitsize, 1);
  2428.   precision = TYPE_PRECISION (unsigned_type);
  2429.  
  2430.   mask = build_int_2 (~0, ~0);
  2431.   TREE_TYPE (mask) = unsigned_type;
  2432.   force_fit_type (mask, 0);
  2433.   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
  2434.   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
  2435.  
  2436.   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
  2437.   if (and_mask != 0)
  2438.     mask = fold (build (BIT_AND_EXPR, unsigned_type,
  2439.             convert (unsigned_type, and_mask), mask));
  2440.  
  2441.   *pmask = mask;
  2442.   return inner;
  2443. }
  2444.  
  2445. /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
  2446.    bit positions.  */
  2447.  
  2448. static int
  2449. all_ones_mask_p (mask, size)
  2450.      tree mask;
  2451.      int size;
  2452. {
  2453.   tree type = TREE_TYPE (mask);
  2454.   int precision = TYPE_PRECISION (type);
  2455.   tree tmask;
  2456.  
  2457.   tmask = build_int_2 (~0, ~0);
  2458.   TREE_TYPE (tmask) = signed_type (type);
  2459.   force_fit_type (tmask, 0);
  2460.   return
  2461.     operand_equal_p (mask, 
  2462.              const_binop (RSHIFT_EXPR,
  2463.                   const_binop (LSHIFT_EXPR, tmask,
  2464.                            size_int (precision - size), 0),
  2465.                   size_int (precision - size), 0),
  2466.              0);
  2467. }
  2468.  
  2469. /* Subroutine for fold_truthop: determine if an operand is simple enough
  2470.    to be evaluated unconditionally.  */
  2471.  
  2472. static int 
  2473. simple_operand_p (exp)
  2474.      tree exp;
  2475. {
  2476.   /* Strip any conversions that don't change the machine mode.  */
  2477.   while ((TREE_CODE (exp) == NOP_EXPR
  2478.       || TREE_CODE (exp) == CONVERT_EXPR)
  2479.      && (TYPE_MODE (TREE_TYPE (exp))
  2480.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
  2481.     exp = TREE_OPERAND (exp, 0);
  2482.  
  2483.   return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
  2484.       || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
  2485.           && ! TREE_ADDRESSABLE (exp)
  2486.           && ! TREE_THIS_VOLATILE (exp)
  2487.           && ! DECL_NONLOCAL (exp)
  2488.           /* Don't regard global variables as simple.  They may be
  2489.          allocated in ways unknown to the compiler (shared memory,
  2490.          #pragma weak, etc).  */
  2491.           && ! TREE_PUBLIC (exp)
  2492.           && ! DECL_EXTERNAL (exp)
  2493.           /* Loading a static variable is unduly expensive, but global
  2494.          registers aren't expensive.  */
  2495.           && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
  2496. }
  2497.  
  2498. /* Subroutine for fold_truthop: try to optimize a range test.
  2499.  
  2500.    For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
  2501.  
  2502.    JCODE is the logical combination of the two terms.  It is TRUTH_AND_EXPR
  2503.    (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
  2504.    (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR).  TYPE is the type of
  2505.    the result.
  2506.  
  2507.    VAR is the value being tested.  LO_CODE and HI_CODE are the comparison
  2508.    operators comparing VAR to LO_CST and HI_CST.  LO_CST is known to be no
  2509.    larger than HI_CST (they may be equal).
  2510.  
  2511.    We return the simplified tree or 0 if no optimization is possible.  */
  2512.  
  2513. static tree
  2514. range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
  2515.      enum tree_code jcode, lo_code, hi_code;
  2516.      tree type, var, lo_cst, hi_cst;
  2517. {
  2518.   tree utype;
  2519.   enum tree_code rcode;
  2520.  
  2521.   /* See if this is a range test and normalize the constant terms.  */
  2522.  
  2523.   if (jcode == TRUTH_AND_EXPR)
  2524.     {
  2525.       switch (lo_code)
  2526.     {
  2527.     case NE_EXPR:
  2528.       /* See if we have VAR != CST && VAR != CST+1.  */
  2529.       if (! (hi_code == NE_EXPR
  2530.          && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
  2531.          && tree_int_cst_equal (integer_one_node,
  2532.                     const_binop (MINUS_EXPR,
  2533.                              hi_cst, lo_cst, 0))))
  2534.         return 0;
  2535.  
  2536.       rcode = GT_EXPR;
  2537.       break;
  2538.  
  2539.     case GT_EXPR:
  2540.     case GE_EXPR:
  2541.       if (hi_code == LT_EXPR)
  2542.         hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
  2543.       else if (hi_code != LE_EXPR)
  2544.         return 0;
  2545.  
  2546.       if (lo_code == GT_EXPR)
  2547.         lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
  2548.  
  2549.       /* We now have VAR >= LO_CST && VAR <= HI_CST.  */
  2550.       rcode = LE_EXPR;
  2551.       break;
  2552.  
  2553.     default:
  2554.       return 0;
  2555.     }
  2556.     }
  2557.   else
  2558.     {
  2559.       switch (lo_code)
  2560.     {
  2561.     case EQ_EXPR:
  2562.       /* See if we have VAR == CST || VAR == CST+1.  */
  2563.       if (! (hi_code == EQ_EXPR
  2564.          && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
  2565.          && tree_int_cst_equal (integer_one_node,
  2566.                     const_binop (MINUS_EXPR,
  2567.                              hi_cst, lo_cst, 0))))
  2568.         return 0;
  2569.  
  2570.       rcode = LE_EXPR;
  2571.       break;
  2572.  
  2573.     case LE_EXPR:
  2574.     case LT_EXPR:
  2575.       if (hi_code == GE_EXPR)
  2576.         hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
  2577.       else if (hi_code != GT_EXPR)
  2578.         return 0;
  2579.  
  2580.       if (lo_code == LE_EXPR)
  2581.         lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
  2582.  
  2583.       /* We now have VAR < LO_CST || VAR > HI_CST.  */
  2584.       rcode = GT_EXPR;
  2585.       break;
  2586.  
  2587.     default:
  2588.       return 0;
  2589.     }
  2590.     }
  2591.  
  2592.   /* When normalizing, it is possible to both increment the smaller constant
  2593.      and decrement the larger constant.  See if they are still ordered.  */
  2594.   if (tree_int_cst_lt (hi_cst, lo_cst))
  2595.     return 0;
  2596.  
  2597.   /* Fail if VAR isn't an integer.  */
  2598.   utype = TREE_TYPE (var);
  2599.   if (! INTEGRAL_TYPE_P (utype))
  2600.     return 0;
  2601.  
  2602.   /* The range test is invalid if subtracting the two constants results
  2603.      in overflow.  This can happen in traditional mode.  */
  2604.   if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
  2605.       || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
  2606.     return 0;
  2607.  
  2608.   if (! TREE_UNSIGNED (utype))
  2609.     {
  2610.       utype = unsigned_type (utype);
  2611.       var = convert (utype, var);
  2612.       lo_cst = convert (utype, lo_cst);
  2613.       hi_cst = convert (utype, hi_cst);
  2614.     }
  2615.  
  2616.   return fold (convert (type,
  2617.             build (rcode, utype,
  2618.                    build (MINUS_EXPR, utype, var, lo_cst),
  2619.                    const_binop (MINUS_EXPR, hi_cst, lo_cst, 0))));
  2620. }
  2621.  
  2622. /* Find ways of folding logical expressions of LHS and RHS:
  2623.    Try to merge two comparisons to the same innermost item.
  2624.    Look for range tests like "ch >= '0' && ch <= '9'".
  2625.    Look for combinations of simple terms on machines with expensive branches
  2626.    and evaluate the RHS unconditionally.
  2627.  
  2628.    For example, if we have p->a == 2 && p->b == 4 and we can make an
  2629.    object large enough to span both A and B, we can do this with a comparison
  2630.    against the object ANDed with the a mask.
  2631.  
  2632.    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
  2633.    operations to do this with one comparison.
  2634.  
  2635.    We check for both normal comparisons and the BIT_AND_EXPRs made this by
  2636.    function and the one above.
  2637.  
  2638.    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
  2639.    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
  2640.  
  2641.    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
  2642.    two operands.
  2643.  
  2644.    We return the simplified tree or 0 if no optimization is possible.  */
  2645.  
  2646. static tree
  2647. fold_truthop (code, truth_type, lhs, rhs)
  2648.      enum tree_code code;
  2649.      tree truth_type, lhs, rhs;
  2650. {
  2651.   /* If this is the "or" of two comparisons, we can do something if we
  2652.      the comparisons are NE_EXPR.  If this is the "and", we can do something
  2653.      if the comparisons are EQ_EXPR.  I.e., 
  2654.          (a->b == 2 && a->c == 4) can become (a->new == NEW).
  2655.  
  2656.      WANTED_CODE is this operation code.  For single bit fields, we can
  2657.      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
  2658.      comparison for one-bit fields.  */
  2659.  
  2660.   enum tree_code wanted_code;
  2661.   enum tree_code lcode, rcode;
  2662.   tree ll_arg, lr_arg, rl_arg, rr_arg;
  2663.   tree ll_inner, lr_inner, rl_inner, rr_inner;
  2664.   int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
  2665.   int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
  2666.   int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
  2667.   int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
  2668.   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
  2669.   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
  2670.   enum machine_mode lnmode, rnmode;
  2671.   tree ll_mask, lr_mask, rl_mask, rr_mask;
  2672.   tree l_const, r_const;
  2673.   tree type, result;
  2674.   int first_bit, end_bit;
  2675.   int volatilep;
  2676.  
  2677.   /* Start by getting the comparison codes and seeing if this looks like
  2678.      a range test.  Fail if anything is volatile.  If one operand is a
  2679.      BIT_AND_EXPR with the constant one, treat it as if it were surrounded
  2680.      with a NE_EXPR.  */
  2681.  
  2682.   if (TREE_SIDE_EFFECTS (lhs)
  2683.       || TREE_SIDE_EFFECTS (rhs))
  2684.     return 0;
  2685.  
  2686.   lcode = TREE_CODE (lhs);
  2687.   rcode = TREE_CODE (rhs);
  2688.  
  2689.   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
  2690.     lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
  2691.  
  2692.   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
  2693.     rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
  2694.  
  2695.   if (TREE_CODE_CLASS (lcode) != '<'
  2696.       || TREE_CODE_CLASS (rcode) != '<')
  2697.     return 0;
  2698.  
  2699.   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
  2700.       ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
  2701.  
  2702.   ll_arg = TREE_OPERAND (lhs, 0);
  2703.   lr_arg = TREE_OPERAND (lhs, 1);
  2704.   rl_arg = TREE_OPERAND (rhs, 0);
  2705.   rr_arg = TREE_OPERAND (rhs, 1);
  2706.   
  2707.   if (TREE_CODE (lr_arg) == INTEGER_CST
  2708.       && TREE_CODE (rr_arg) == INTEGER_CST
  2709.       && operand_equal_p (ll_arg, rl_arg, 0))
  2710.     {
  2711.       if (tree_int_cst_lt (lr_arg, rr_arg))
  2712.     result = range_test (code, truth_type, lcode, rcode,
  2713.                  ll_arg, lr_arg, rr_arg);
  2714.       else
  2715.     result = range_test (code, truth_type, rcode, lcode,
  2716.                  ll_arg, rr_arg, lr_arg);
  2717.  
  2718.       /* If this isn't a range test, it also isn't a comparison that
  2719.      can be merged.  However, it wins to evaluate the RHS unconditionally
  2720.      on machines with expensive branches.   */
  2721.  
  2722.       if (result == 0 && BRANCH_COST >= 2)
  2723.     {
  2724.       if (TREE_CODE (ll_arg) != VAR_DECL
  2725.           && TREE_CODE (ll_arg) != PARM_DECL)
  2726.         {
  2727.           /* Avoid evaluating the variable part twice.  */
  2728.           ll_arg = save_expr (ll_arg);
  2729.           lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
  2730.           rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
  2731.         }
  2732.       return build (code, truth_type, lhs, rhs);
  2733.     }
  2734.       return result;
  2735.     }
  2736.  
  2737.   /* If the RHS can be evaluated unconditionally and its operands are
  2738.      simple, it wins to evaluate the RHS unconditionally on machines
  2739.      with expensive branches.  In this case, this isn't a comparison
  2740.      that can be merged.  */
  2741.  
  2742.   /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
  2743.      are with zero (tmw).  */
  2744.  
  2745.   if (BRANCH_COST >= 2
  2746.       && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
  2747.       && simple_operand_p (rl_arg)
  2748.       && simple_operand_p (rr_arg))
  2749.     return build (code, truth_type, lhs, rhs);
  2750.  
  2751.   /* See if the comparisons can be merged.  Then get all the parameters for
  2752.      each side.  */
  2753.  
  2754.   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
  2755.       || (rcode != EQ_EXPR && rcode != NE_EXPR))
  2756.     return 0;
  2757.  
  2758.   volatilep = 0;
  2759.   ll_inner = decode_field_reference (ll_arg,
  2760.                      &ll_bitsize, &ll_bitpos, &ll_mode,
  2761.                      &ll_unsignedp, &volatilep, &ll_mask);
  2762.   lr_inner = decode_field_reference (lr_arg,
  2763.                      &lr_bitsize, &lr_bitpos, &lr_mode,
  2764.                      &lr_unsignedp, &volatilep, &lr_mask);
  2765.   rl_inner = decode_field_reference (rl_arg,
  2766.                      &rl_bitsize, &rl_bitpos, &rl_mode,
  2767.                      &rl_unsignedp, &volatilep, &rl_mask);
  2768.   rr_inner = decode_field_reference (rr_arg,
  2769.                      &rr_bitsize, &rr_bitpos, &rr_mode,
  2770.                      &rr_unsignedp, &volatilep, &rr_mask);
  2771.  
  2772.   /* It must be true that the inner operation on the lhs of each
  2773.      comparison must be the same if we are to be able to do anything.
  2774.      Then see if we have constants.  If not, the same must be true for
  2775.      the rhs's.  */
  2776.   if (volatilep || ll_inner == 0 || rl_inner == 0
  2777.       || ! operand_equal_p (ll_inner, rl_inner, 0))
  2778.     return 0;
  2779.  
  2780.   if (TREE_CODE (lr_arg) == INTEGER_CST
  2781.       && TREE_CODE (rr_arg) == INTEGER_CST)
  2782.     l_const = lr_arg, r_const = rr_arg;
  2783.   else if (lr_inner == 0 || rr_inner == 0
  2784.        || ! operand_equal_p (lr_inner, rr_inner, 0))
  2785.     return 0;
  2786.   else
  2787.     l_const = r_const = 0;
  2788.  
  2789.   /* If either comparison code is not correct for our logical operation,
  2790.      fail.  However, we can convert a one-bit comparison against zero into
  2791.      the opposite comparison against that bit being set in the field.  */
  2792.  
  2793.   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
  2794.   if (lcode != wanted_code)
  2795.     {
  2796.       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
  2797.     l_const = ll_mask;
  2798.       else
  2799.     return 0;
  2800.     }
  2801.  
  2802.   if (rcode != wanted_code)
  2803.     {
  2804.       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
  2805.     r_const = rl_mask;
  2806.       else
  2807.     return 0;
  2808.     }
  2809.  
  2810.   /* See if we can find a mode that contains both fields being compared on
  2811.      the left.  If we can't, fail.  Otherwise, update all constants and masks
  2812.      to be relative to a field of that size.  */
  2813.   first_bit = MIN (ll_bitpos, rl_bitpos);
  2814.   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
  2815.   lnmode = get_best_mode (end_bit - first_bit, first_bit,
  2816.               TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
  2817.               volatilep);
  2818.   if (lnmode == VOIDmode)
  2819.     return 0;
  2820.  
  2821.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  2822.   lnbitpos = first_bit & ~ (lnbitsize - 1);
  2823.   type = type_for_size (lnbitsize, 1);
  2824.   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
  2825.  
  2826. #if BYTES_BIG_ENDIAN
  2827.   xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
  2828.   xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
  2829. #endif
  2830.  
  2831.   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
  2832.              size_int (xll_bitpos), 0);
  2833.   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
  2834.              size_int (xrl_bitpos), 0);
  2835.  
  2836.   /* Make sure the constants are interpreted as unsigned, so we
  2837.      don't have sign bits outside the range of their type.  */
  2838.  
  2839.   if (l_const)
  2840.     {
  2841.       l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
  2842.       l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
  2843.                  size_int (xll_bitpos), 0);
  2844.     }
  2845.   if (r_const)
  2846.     {
  2847.       r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
  2848.       r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
  2849.                  size_int (xrl_bitpos), 0);
  2850.     }
  2851.  
  2852.   /* If the right sides are not constant, do the same for it.  Also,
  2853.      disallow this optimization if a size or signedness mismatch occurs
  2854.      between the left and right sides.  */
  2855.   if (l_const == 0)
  2856.     {
  2857.       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
  2858.       || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
  2859.       /* Make sure the two fields on the right
  2860.          correspond to the left without being swapped.  */
  2861.       || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
  2862.     return 0;
  2863.  
  2864.       first_bit = MIN (lr_bitpos, rr_bitpos);
  2865.       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
  2866.       rnmode = get_best_mode (end_bit - first_bit, first_bit,
  2867.                   TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
  2868.                   volatilep);
  2869.       if (rnmode == VOIDmode)
  2870.     return 0;
  2871.  
  2872.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  2873.       rnbitpos = first_bit & ~ (rnbitsize - 1);
  2874.       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
  2875.  
  2876. #if BYTES_BIG_ENDIAN
  2877.       xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
  2878.       xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
  2879. #endif
  2880.  
  2881.       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
  2882.                  size_int (xlr_bitpos), 0);
  2883.       rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
  2884.                  size_int (xrr_bitpos), 0);
  2885.  
  2886.       /* Make a mask that corresponds to both fields being compared.
  2887.      Do this for both items being compared.  If the masks agree,
  2888.      we can do this by masking both and comparing the masked
  2889.      results.  */
  2890.       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
  2891.       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
  2892.       if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
  2893.     {
  2894.       lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2895.                     ll_unsignedp || rl_unsignedp);
  2896.       rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
  2897.                     lr_unsignedp || rr_unsignedp);
  2898.       if (! all_ones_mask_p (ll_mask, lnbitsize))
  2899.         {
  2900.           lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
  2901.           rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
  2902.         }
  2903.       return build (wanted_code, truth_type, lhs, rhs);
  2904.     }
  2905.  
  2906.       /* There is still another way we can do something:  If both pairs of
  2907.      fields being compared are adjacent, we may be able to make a wider
  2908.      field containing them both.  */
  2909.       if ((ll_bitsize + ll_bitpos == rl_bitpos
  2910.        && lr_bitsize + lr_bitpos == rr_bitpos)
  2911.       || (ll_bitpos == rl_bitpos + rl_bitsize
  2912.           && lr_bitpos == rr_bitpos + rr_bitsize))
  2913.     return build (wanted_code, truth_type,
  2914.               make_bit_field_ref (ll_inner, type,
  2915.                       ll_bitsize + rl_bitsize,
  2916.                       MIN (ll_bitpos, rl_bitpos),
  2917.                       ll_unsignedp),
  2918.               make_bit_field_ref (lr_inner, type,
  2919.                       lr_bitsize + rr_bitsize,
  2920.                       MIN (lr_bitpos, rr_bitpos),
  2921.                       lr_unsignedp));
  2922.  
  2923.       return 0;
  2924.     }
  2925.  
  2926.   /* Handle the case of comparisons with constants.  If there is something in
  2927.      common between the masks, those bits of the constants must be the same.
  2928.      If not, the condition is always false.  Test for this to avoid generating
  2929.      incorrect code below.  */
  2930.   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
  2931.   if (! integer_zerop (result)
  2932.       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
  2933.                const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
  2934.     {
  2935.       if (wanted_code == NE_EXPR)
  2936.     {
  2937.       warning ("`or' of unmatched not-equal tests is always 1");
  2938.       return convert (truth_type, integer_one_node);
  2939.     }
  2940.       else
  2941.     {
  2942.       warning ("`and' of mutually exclusive equal-tests is always zero");
  2943.       return convert (truth_type, integer_zero_node);
  2944.     }
  2945.     }
  2946.  
  2947.   /* Construct the expression we will return.  First get the component
  2948.      reference we will make.  Unless the mask is all ones the width of
  2949.      that field, perform the mask operation.  Then compare with the
  2950.      merged constant.  */
  2951.   result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2952.                    ll_unsignedp || rl_unsignedp);
  2953.  
  2954.   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
  2955.   if (! all_ones_mask_p (ll_mask, lnbitsize))
  2956.     result = build (BIT_AND_EXPR, type, result, ll_mask);
  2957.  
  2958.   return build (wanted_code, truth_type, result,
  2959.         const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
  2960. }
  2961.  
  2962. /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
  2963.    S, a SAVE_EXPR, return the expression actually being evaluated.   Note
  2964.    that we may sometimes modify the tree.  */
  2965.  
  2966. static tree
  2967. strip_compound_expr (t, s)
  2968.      tree t;
  2969.      tree s;
  2970. {
  2971.   tree type = TREE_TYPE (t);
  2972.   enum tree_code code = TREE_CODE (t);
  2973.  
  2974.   /* See if this is the COMPOUND_EXPR we want to eliminate.  */
  2975.   if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
  2976.       && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
  2977.     return TREE_OPERAND (t, 1);
  2978.  
  2979.   /* See if this is a COND_EXPR or a simple arithmetic operator.   We
  2980.      don't bother handling any other types.  */
  2981.   else if (code == COND_EXPR)
  2982.     {
  2983.       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2984.       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
  2985.       TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
  2986.     }
  2987.   else if (TREE_CODE_CLASS (code) == '1')
  2988.     TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2989.   else if (TREE_CODE_CLASS (code) == '<'
  2990.        || TREE_CODE_CLASS (code) == '2')
  2991.     {
  2992.       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2993.       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
  2994.     }
  2995.  
  2996.   return t;
  2997. }
  2998.  
  2999. /* Perform constant folding and related simplification of EXPR.
  3000.    The related simplifications include x*1 => x, x*0 => 0, etc.,
  3001.    and application of the associative law.
  3002.    NOP_EXPR conversions may be removed freely (as long as we
  3003.    are careful not to change the C type of the overall expression)
  3004.    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
  3005.    but we can constant-fold them if they have constant operands.  */
  3006.  
  3007. tree
  3008. fold (expr) 
  3009.      tree expr;
  3010. {
  3011.   register tree t = expr;
  3012.   tree t1 = NULL_TREE;
  3013.   tree tem;
  3014.   tree type = TREE_TYPE (expr);
  3015.   register tree arg0, arg1;
  3016.   register enum tree_code code = TREE_CODE (t);
  3017.   register int kind;
  3018.   int invert;
  3019.  
  3020.   /* WINS will be nonzero when the switch is done
  3021.      if all operands are constant.  */
  3022.  
  3023.   int wins = 1;
  3024.  
  3025.   /* Don't try to process an RTL_EXPR since its operands aren't trees.  */
  3026.   if (code == RTL_EXPR)
  3027.     return t;
  3028.  
  3029.   /* Return right away if already constant.  */
  3030.   if (TREE_CONSTANT (t))
  3031.     {
  3032.       if (code == CONST_DECL)
  3033.     return DECL_INITIAL (t);
  3034.       return t;
  3035.     }
  3036.   
  3037.   kind = TREE_CODE_CLASS (code);
  3038.   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
  3039.     {
  3040.       tree subop;
  3041.  
  3042.       /* Special case for conversion ops that can have fixed point args.  */
  3043.       arg0 = TREE_OPERAND (t, 0);
  3044.  
  3045.       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
  3046.       if (arg0 != 0)
  3047.     STRIP_TYPE_NOPS (arg0);
  3048.  
  3049.       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
  3050.     subop = TREE_REALPART (arg0);
  3051.       else
  3052.     subop = arg0;
  3053.  
  3054.       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
  3055. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3056.       && TREE_CODE (subop) != REAL_CST
  3057. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3058.       )
  3059.     /* Note that TREE_CONSTANT isn't enough:
  3060.        static var addresses are constant but we can't
  3061.        do arithmetic on them.  */
  3062.     wins = 0;
  3063.     }
  3064.   else if (kind == 'e' || kind == '<'
  3065.        || kind == '1' || kind == '2' || kind == 'r')
  3066.     {
  3067.       register int len = tree_code_length[(int) code];
  3068.       register int i;
  3069.       for (i = 0; i < len; i++)
  3070.     {
  3071.       tree op = TREE_OPERAND (t, i);
  3072.       tree subop;
  3073.  
  3074.       if (op == 0)
  3075.         continue;        /* Valid for CALL_EXPR, at least.  */
  3076.  
  3077.       if (kind == '<' || code == RSHIFT_EXPR)
  3078.         {
  3079.           /* Signedness matters here.  Perhaps we can refine this
  3080.          later.  */
  3081.           STRIP_TYPE_NOPS (op);
  3082.         }
  3083.       else
  3084.         {
  3085.           /* Strip any conversions that don't change the mode.  */
  3086.           STRIP_NOPS (op);
  3087.         }
  3088.       
  3089.       if (TREE_CODE (op) == COMPLEX_CST)
  3090.         subop = TREE_REALPART (op);
  3091.       else
  3092.         subop = op;
  3093.  
  3094.       if (TREE_CODE (subop) != INTEGER_CST
  3095. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3096.           && TREE_CODE (subop) != REAL_CST
  3097. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3098.           )
  3099.         /* Note that TREE_CONSTANT isn't enough:
  3100.            static var addresses are constant but we can't
  3101.            do arithmetic on them.  */
  3102.         wins = 0;
  3103.  
  3104.       if (i == 0)
  3105.         arg0 = op;
  3106.       else if (i == 1)
  3107.         arg1 = op;
  3108.     }
  3109.     }
  3110.  
  3111.   /* If this is a commutative operation, and ARG0 is a constant, move it
  3112.      to ARG1 to reduce the number of tests below.  */
  3113.   if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
  3114.        || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
  3115.        || code == BIT_AND_EXPR)
  3116.       && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
  3117.     {
  3118.       tem = arg0; arg0 = arg1; arg1 = tem;
  3119.  
  3120.       tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
  3121.       TREE_OPERAND (t, 1) = tem;
  3122.     }
  3123.  
  3124.   /* Now WINS is set as described above,
  3125.      ARG0 is the first operand of EXPR,
  3126.      and ARG1 is the second operand (if it has more than one operand).
  3127.  
  3128.      First check for cases where an arithmetic operation is applied to a
  3129.      compound, conditional, or comparison operation.  Push the arithmetic
  3130.      operation inside the compound or conditional to see if any folding
  3131.      can then be done.  Convert comparison to conditional for this purpose.
  3132.      The also optimizes non-constant cases that used to be done in
  3133.      expand_expr.
  3134.  
  3135.      Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
  3136.      one of the operands is a comparison and the other is a comparison, a
  3137.      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
  3138.      code below would make the expression more complex.  Change it to a
  3139.      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to 
  3140.      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
  3141.  
  3142.   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
  3143.        || code == EQ_EXPR || code == NE_EXPR)
  3144.       && ((truth_value_p (TREE_CODE (arg0))
  3145.        && (truth_value_p (TREE_CODE (arg1))
  3146.            || (TREE_CODE (arg1) == BIT_AND_EXPR
  3147.            && integer_onep (TREE_OPERAND (arg1, 1)))))
  3148.       || (truth_value_p (TREE_CODE (arg1))
  3149.           && (truth_value_p (TREE_CODE (arg0))
  3150.           || (TREE_CODE (arg0) == BIT_AND_EXPR
  3151.               && integer_onep (TREE_OPERAND (arg0, 1)))))))
  3152.     {
  3153.       t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
  3154.                : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
  3155.                : TRUTH_XOR_EXPR,
  3156.                type, arg0, arg1));
  3157.  
  3158.       if (code == EQ_EXPR)
  3159.     t = invert_truthvalue (t);
  3160.  
  3161.       return t;
  3162.     }
  3163.  
  3164.   if (TREE_CODE_CLASS (code) == '1')
  3165.     {
  3166.       if (TREE_CODE (arg0) == COMPOUND_EXPR)
  3167.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3168.               fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
  3169.       else if (TREE_CODE (arg0) == COND_EXPR)
  3170.     {
  3171.       t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
  3172.                fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
  3173.                fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
  3174.  
  3175.       /* If this was a conversion, and all we did was to move into
  3176.          inside the COND_EXPR, bring it back out.  Then return so we
  3177.          don't get into an infinite recursion loop taking the conversion
  3178.          out and then back in.  */
  3179.  
  3180.       if ((code == NOP_EXPR || code == CONVERT_EXPR
  3181.            || code == NON_LVALUE_EXPR)
  3182.           && TREE_CODE (t) == COND_EXPR
  3183.           && TREE_CODE (TREE_OPERAND (t, 1)) == code
  3184.           && TREE_CODE (TREE_OPERAND (t, 2)) == code
  3185.           && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
  3186.           == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))))
  3187.         t = build1 (code, type,
  3188.             build (COND_EXPR,
  3189.                    TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
  3190.                    TREE_OPERAND (t, 0),
  3191.                    TREE_OPERAND (TREE_OPERAND (t, 1), 0),
  3192.                    TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
  3193.       return t;
  3194.     }
  3195.       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') 
  3196.     return fold (build (COND_EXPR, type, arg0,
  3197.                 fold (build1 (code, type, integer_one_node)),
  3198.                 fold (build1 (code, type, integer_zero_node))));
  3199.    }
  3200.   else if (TREE_CODE_CLASS (code) == '2'
  3201.        || TREE_CODE_CLASS (code) == '<')
  3202.     {
  3203.       if (TREE_CODE (arg1) == COMPOUND_EXPR)
  3204.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
  3205.               fold (build (code, type,
  3206.                    arg0, TREE_OPERAND (arg1, 1))));
  3207.       else if (TREE_CODE (arg1) == COND_EXPR
  3208.            || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
  3209.     {
  3210.       tree test, true_value, false_value;
  3211.  
  3212.       if (TREE_CODE (arg1) == COND_EXPR)
  3213.         {
  3214.           test = TREE_OPERAND (arg1, 0);
  3215.           true_value = TREE_OPERAND (arg1, 1);
  3216.           false_value = TREE_OPERAND (arg1, 2);
  3217.         }
  3218.       else
  3219.         {
  3220.           test = arg1;
  3221.           true_value = integer_one_node;
  3222.           false_value = integer_zero_node;
  3223.         }
  3224.  
  3225.       /* If ARG0 is complex we want to make sure we only evaluate
  3226.          it once.  Though this is only required if it is volatile, it
  3227.          might be more efficient even if it is not.  However, if we
  3228.          succeed in folding one part to a constant, we do not need
  3229.          to make this SAVE_EXPR.  Since we do this optimization
  3230.          primarily to see if we do end up with constant and this
  3231.          SAVE_EXPR interfers with later optimizations, suppressing
  3232.          it when we can is important.  */
  3233.  
  3234.       if (TREE_CODE (arg0) != SAVE_EXPR
  3235.           && ((TREE_CODE (arg0) != VAR_DECL
  3236.            && TREE_CODE (arg0) != PARM_DECL)
  3237.           || TREE_SIDE_EFFECTS (arg0)))
  3238.         {
  3239.           tree lhs = fold (build (code, type, arg0, true_value));
  3240.           tree rhs = fold (build (code, type, arg0, false_value));
  3241.  
  3242.           if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
  3243.         return fold (build (COND_EXPR, type, test, lhs, rhs));
  3244.  
  3245.           arg0 = save_expr (arg0);
  3246.         }
  3247.  
  3248.       test = fold (build (COND_EXPR, type, test,
  3249.                   fold (build (code, type, arg0, true_value)),
  3250.                   fold (build (code, type, arg0, false_value))));
  3251.       if (TREE_CODE (arg0) == SAVE_EXPR)
  3252.         return build (COMPOUND_EXPR, type,
  3253.               convert (void_type_node, arg0),
  3254.               strip_compound_expr (test, arg0));
  3255.       else
  3256.         return convert (type, test);
  3257.     }
  3258.  
  3259.       else if (TREE_CODE (arg0) == COMPOUND_EXPR)
  3260.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3261.               fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
  3262.       else if (TREE_CODE (arg0) == COND_EXPR
  3263.            || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
  3264.     {
  3265.       tree test, true_value, false_value;
  3266.  
  3267.       if (TREE_CODE (arg0) == COND_EXPR)
  3268.         {
  3269.           test = TREE_OPERAND (arg0, 0);
  3270.           true_value = TREE_OPERAND (arg0, 1);
  3271.           false_value = TREE_OPERAND (arg0, 2);
  3272.         }
  3273.       else
  3274.         {
  3275.           test = arg0;
  3276.           true_value = integer_one_node;
  3277.           false_value = integer_zero_node;
  3278.         }
  3279.  
  3280.       if (TREE_CODE (arg1) != SAVE_EXPR
  3281.           && ((TREE_CODE (arg1) != VAR_DECL
  3282.            && TREE_CODE (arg1) != PARM_DECL)
  3283.           || TREE_SIDE_EFFECTS (arg1)))
  3284.         {
  3285.           tree lhs = fold (build (code, type, true_value, arg1));
  3286.           tree rhs = fold (build (code, type, false_value, arg1));
  3287.  
  3288.           if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)
  3289.           || TREE_CONSTANT (arg1))
  3290.         return fold (build (COND_EXPR, type, test, lhs, rhs));
  3291.  
  3292.           arg1 = save_expr (arg1);
  3293.         }
  3294.  
  3295.       test = fold (build (COND_EXPR, type, test,
  3296.                   fold (build (code, type, true_value, arg1)),
  3297.                   fold (build (code, type, false_value, arg1))));
  3298.       if (TREE_CODE (arg1) == SAVE_EXPR)
  3299.         return build (COMPOUND_EXPR, type,
  3300.               convert (void_type_node, arg1),
  3301.               strip_compound_expr (test, arg1));
  3302.       else
  3303.         return convert (type, test);
  3304.     }
  3305.     }
  3306.   else if (TREE_CODE_CLASS (code) == '<'
  3307.        && TREE_CODE (arg0) == COMPOUND_EXPR)
  3308.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3309.           fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
  3310.   else if (TREE_CODE_CLASS (code) == '<'
  3311.        && TREE_CODE (arg1) == COMPOUND_EXPR)
  3312.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
  3313.           fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
  3314.       
  3315.   switch (code)
  3316.     {
  3317.     case INTEGER_CST:
  3318.     case REAL_CST:
  3319.     case STRING_CST:
  3320.     case COMPLEX_CST:
  3321.     case CONSTRUCTOR:
  3322.       return t;
  3323.  
  3324.     case CONST_DECL:
  3325.       return fold (DECL_INITIAL (t));
  3326.  
  3327.     case NOP_EXPR:
  3328.     case FLOAT_EXPR:
  3329.     case CONVERT_EXPR:
  3330.     case FIX_TRUNC_EXPR:
  3331.       /* Other kinds of FIX are not handled properly by fold_convert.  */
  3332.  
  3333.       /* In addition to the cases of two conversions in a row 
  3334.      handled below, if we are converting something to its own
  3335.      type via an object of identical or wider precision, neither
  3336.      conversion is needed.  */
  3337.       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
  3338.        || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
  3339.       && TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == TREE_TYPE (t)
  3340.       && ((INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
  3341.            && INTEGRAL_TYPE_P (TREE_TYPE (t)))
  3342.           || (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
  3343.           && FLOAT_TYPE_P (TREE_TYPE (t))))
  3344.       && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3345.           >= TYPE_PRECISION (TREE_TYPE (t))))
  3346.     return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
  3347.  
  3348.       /* Two conversions in a row are not needed unless:
  3349.      - the intermediate type is narrower than both initial and final, or
  3350.      - the intermediate type and innermost type differ in signedness,
  3351.        and the outermost type is wider than the intermediate, or
  3352.      - the initial type is a pointer type and the precisions of the
  3353.        intermediate and final types differ, or
  3354.      - the final type is a pointer type and the precisions of the 
  3355.       initial and intermediate types differ.  */
  3356.       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
  3357.        || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
  3358.       && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3359.           > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3360.           ||
  3361.           TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3362.           > TYPE_PRECISION (TREE_TYPE (t)))
  3363.       && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3364.          == INTEGER_TYPE)
  3365.         && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
  3366.             == INTEGER_TYPE)
  3367.         && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
  3368.             != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3369.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3370.             < TYPE_PRECISION (TREE_TYPE (t))))
  3371.       && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
  3372.            && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3373.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
  3374.           ==
  3375.           (TREE_UNSIGNED (TREE_TYPE (t))
  3376.            && (TYPE_PRECISION (TREE_TYPE (t))
  3377.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  3378.       && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3379.          == POINTER_TYPE)
  3380.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3381.             != TYPE_PRECISION (TREE_TYPE (t))))
  3382.       && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
  3383.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3384.             != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  3385.     return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
  3386.  
  3387.       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
  3388.       && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
  3389.       /* Detect assigning a bitfield.  */
  3390.       && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
  3391.            && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
  3392.     {
  3393.       /* Don't leave an assignment inside a conversion
  3394.          unless assigning a bitfield.  */
  3395.       tree prev = TREE_OPERAND (t, 0);
  3396.       TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
  3397.       /* First do the assignment, then return converted constant.  */
  3398.       t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
  3399.       TREE_USED (t) = 1;
  3400.       return t;
  3401.     }
  3402.       if (!wins)
  3403.     {
  3404.       TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
  3405.       return t;
  3406.     }
  3407.       return fold_convert (t, arg0);
  3408.  
  3409. #if 0  /* This loses on &"foo"[0].  */
  3410.     case ARRAY_REF:
  3411.     {
  3412.       int i;
  3413.  
  3414.       /* Fold an expression like: "foo"[2] */
  3415.       if (TREE_CODE (arg0) == STRING_CST
  3416.           && TREE_CODE (arg1) == INTEGER_CST
  3417.           && !TREE_INT_CST_HIGH (arg1)
  3418.           && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
  3419.         {
  3420.           t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
  3421.           TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
  3422.           force_fit_type (t, 0);
  3423.         }
  3424.     }
  3425.       return t;
  3426. #endif /* 0 */
  3427.  
  3428.     case COMPONENT_REF:
  3429.       if (TREE_CODE (arg0) == CONSTRUCTOR)
  3430.     {
  3431.       tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
  3432.       if (m)
  3433.         t = TREE_VALUE (m);
  3434.     }
  3435.       return t;
  3436.  
  3437.     case RANGE_EXPR:
  3438.       TREE_CONSTANT (t) = wins;
  3439.       return t;
  3440.  
  3441.     case NEGATE_EXPR:
  3442.       if (wins)
  3443.     {
  3444.       if (TREE_CODE (arg0) == INTEGER_CST)
  3445.         {
  3446.           HOST_WIDE_INT low, high;
  3447.           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
  3448.                      TREE_INT_CST_HIGH (arg0),
  3449.                      &low, &high);
  3450.           t = build_int_2 (low, high);
  3451.           TREE_TYPE (t) = type;
  3452.           TREE_OVERFLOW (t)
  3453.         = (TREE_OVERFLOW (arg0)
  3454.            | force_fit_type (t, overflow));
  3455.           TREE_CONSTANT_OVERFLOW (t)
  3456.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
  3457.         }
  3458.       else if (TREE_CODE (arg0) == REAL_CST)
  3459.         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  3460.       TREE_TYPE (t) = type;
  3461.     }
  3462.       else if (TREE_CODE (arg0) == NEGATE_EXPR)
  3463.     return TREE_OPERAND (arg0, 0);
  3464.  
  3465.       /* Convert - (a - b) to (b - a) for non-floating-point.  */
  3466.       else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
  3467.     return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
  3468.               TREE_OPERAND (arg0, 0));
  3469.  
  3470.       return t;
  3471.  
  3472.     case ABS_EXPR:
  3473.       if (wins)
  3474.     {
  3475.       if (TREE_CODE (arg0) == INTEGER_CST)
  3476.         {
  3477.           if (! TREE_UNSIGNED (type)
  3478.           && TREE_INT_CST_HIGH (arg0) < 0)
  3479.         {
  3480.           HOST_WIDE_INT low, high;
  3481.           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
  3482.                          TREE_INT_CST_HIGH (arg0),
  3483.                          &low, &high);
  3484.           t = build_int_2 (low, high);
  3485.           TREE_TYPE (t) = type;
  3486.           TREE_OVERFLOW (t)
  3487.             = (TREE_OVERFLOW (arg0)
  3488.                | force_fit_type (t, overflow));
  3489.           TREE_CONSTANT_OVERFLOW (t)
  3490.             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
  3491.         }
  3492.         }
  3493.       else if (TREE_CODE (arg0) == REAL_CST)
  3494.         {
  3495.           if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
  3496.         t = build_real (type,
  3497.                 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  3498.         }
  3499.       TREE_TYPE (t) = type;
  3500.     }
  3501.       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
  3502.     return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
  3503.       return t;
  3504.  
  3505.     case CONJ_EXPR:
  3506.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  3507.     return arg0;
  3508.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  3509.     return build (COMPLEX_EXPR, TREE_TYPE (arg0),
  3510.               TREE_OPERAND (arg0, 0),
  3511.               fold (build1 (NEGATE_EXPR,
  3512.                     TREE_TYPE (TREE_TYPE (arg0)),
  3513.                     TREE_OPERAND (arg0, 1))));
  3514.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  3515.     return build_complex (TREE_OPERAND (arg0, 0),
  3516.                   fold (build1 (NEGATE_EXPR,
  3517.                         TREE_TYPE (TREE_TYPE (arg0)),
  3518.                         TREE_OPERAND (arg0, 1))));
  3519.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  3520.     return fold (build (TREE_CODE (arg0), type,
  3521.                 fold (build1 (CONJ_EXPR, type,
  3522.                       TREE_OPERAND (arg0, 0))),
  3523.                 fold (build1 (CONJ_EXPR,
  3524.                       type, TREE_OPERAND (arg0, 1)))));
  3525.       else if (TREE_CODE (arg0) == CONJ_EXPR)
  3526.     return TREE_OPERAND (arg0, 0);
  3527.       return t;
  3528.  
  3529.     case BIT_NOT_EXPR:
  3530.       if (wins)
  3531.     {
  3532.       if (TREE_CODE (arg0) == INTEGER_CST)
  3533.         t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
  3534.                  ~ TREE_INT_CST_HIGH (arg0));
  3535.       TREE_TYPE (t) = type;
  3536.       force_fit_type (t, 0);
  3537.       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
  3538.       TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
  3539.     }
  3540.       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
  3541.     return TREE_OPERAND (arg0, 0);
  3542.       return t;
  3543.  
  3544.     case PLUS_EXPR:
  3545.       /* A + (-B) -> A - B */
  3546.       if (TREE_CODE (arg1) == NEGATE_EXPR)
  3547.     return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  3548.       else if (! FLOAT_TYPE_P (type))
  3549.     {
  3550.       if (integer_zerop (arg1))
  3551.         return non_lvalue (convert (type, arg0));
  3552.  
  3553.       /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
  3554.          with a constant, and the two constants have no bits in common,
  3555.          we should treat this as a BIT_IOR_EXPR since this may produce more
  3556.          simplifications.  */
  3557.       if (TREE_CODE (arg0) == BIT_AND_EXPR
  3558.           && TREE_CODE (arg1) == BIT_AND_EXPR
  3559.           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  3560.           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
  3561.           && integer_zerop (const_binop (BIT_AND_EXPR,
  3562.                          TREE_OPERAND (arg0, 1),
  3563.                          TREE_OPERAND (arg1, 1), 0)))
  3564.         {
  3565.           code = BIT_IOR_EXPR;
  3566.           goto bit_ior;
  3567.         }
  3568.  
  3569.       /* (A * C) + (B * C) -> (A+B) * C.  Since we are most concerned
  3570.          about the case where C is a constant, just try one of the
  3571.          four possibilities.  */
  3572.  
  3573.       if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
  3574.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  3575.                   TREE_OPERAND (arg1, 1), 0))
  3576.         return fold (build (MULT_EXPR, type,
  3577.                 fold (build (PLUS_EXPR, type,
  3578.                          TREE_OPERAND (arg0, 0),
  3579.                          TREE_OPERAND (arg1, 0))),
  3580.                 TREE_OPERAND (arg0, 1)));
  3581.     }
  3582.       /* In IEEE floating point, x+0 may not equal x.  */
  3583.       else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3584.         || flag_fast_math)
  3585.            && real_zerop (arg1))
  3586.     return non_lvalue (convert (type, arg0));
  3587.     associate:
  3588.       /* In most languages, can't associate operations on floats
  3589.      through parentheses.  Rather than remember where the parentheses
  3590.      were, we don't associate floats at all.  It shouldn't matter much.
  3591.      However, associating multiplications is only very slightly
  3592.      inaccurate, so do that if -ffast-math is specified.  */
  3593.       if (FLOAT_TYPE_P (type)
  3594.       && ! (flag_fast_math && code == MULT_EXPR))
  3595.     goto binary;
  3596.  
  3597.       /* The varsign == -1 cases happen only for addition and subtraction.
  3598.      It says that the arg that was split was really CON minus VAR.
  3599.      The rest of the code applies to all associative operations.  */
  3600.       if (!wins)
  3601.     {
  3602.       tree var, con;
  3603.       int varsign;
  3604.  
  3605.       if (split_tree (arg0, code, &var, &con, &varsign))
  3606.         {
  3607.           if (varsign == -1)
  3608.         {
  3609.           /* EXPR is (CON-VAR) +- ARG1.  */
  3610.           /* If it is + and VAR==ARG1, return just CONST.  */
  3611.           if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
  3612.             return convert (TREE_TYPE (t), con);
  3613.             
  3614.           /* If ARG0 is a constant, don't change things around;
  3615.              instead keep all the constant computations together.  */
  3616.  
  3617.           if (TREE_CONSTANT (arg0))
  3618.             return t;
  3619.  
  3620.           /* Otherwise return (CON +- ARG1) - VAR.  */
  3621.           TREE_SET_CODE (t, MINUS_EXPR);
  3622.           TREE_OPERAND (t, 1) = var;
  3623.           TREE_OPERAND (t, 0)
  3624.             = fold (build (code, TREE_TYPE (t), con, arg1));
  3625.         }
  3626.           else
  3627.         {
  3628.           /* EXPR is (VAR+CON) +- ARG1.  */
  3629.           /* If it is - and VAR==ARG1, return just CONST.  */
  3630.           if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
  3631.             return convert (TREE_TYPE (t), con);
  3632.             
  3633.           /* If ARG0 is a constant, don't change things around;
  3634.              instead keep all the constant computations together.  */
  3635.  
  3636.           if (TREE_CONSTANT (arg0))
  3637.             return t;
  3638.  
  3639.           /* Otherwise return VAR +- (ARG1 +- CON).  */
  3640.           TREE_OPERAND (t, 1) = tem
  3641.             = fold (build (code, TREE_TYPE (t), arg1, con));
  3642.           TREE_OPERAND (t, 0) = var;
  3643.           if (integer_zerop (tem)
  3644.               && (code == PLUS_EXPR || code == MINUS_EXPR))
  3645.             return convert (type, var);
  3646.           /* If we have x +/- (c - d) [c an explicit integer]
  3647.              change it to x -/+ (d - c) since if d is relocatable
  3648.              then the latter can be a single immediate insn
  3649.              and the former cannot.  */
  3650.           if (TREE_CODE (tem) == MINUS_EXPR
  3651.               && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
  3652.             {
  3653.               tree tem1 = TREE_OPERAND (tem, 1);
  3654.               TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
  3655.               TREE_OPERAND (tem, 0) = tem1;
  3656.               TREE_SET_CODE (t,
  3657.                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  3658.             }
  3659.         }
  3660.           return t;
  3661.         }
  3662.  
  3663.       if (split_tree (arg1, code, &var, &con, &varsign))
  3664.         {
  3665.           if (TREE_CONSTANT (arg1))
  3666.         return t;
  3667.  
  3668.           if (varsign == -1)
  3669.         TREE_SET_CODE (t,
  3670.                    (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  3671.  
  3672.           /* EXPR is ARG0 +- (CON +- VAR).  */
  3673.           if (TREE_CODE (t) == MINUS_EXPR
  3674.           && operand_equal_p (var, arg0, 0))
  3675.         {
  3676.           /* If VAR and ARG0 cancel, return just CON or -CON.  */
  3677.           if (code == PLUS_EXPR)
  3678.             return convert (TREE_TYPE (t), con);
  3679.           return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
  3680.                        convert (TREE_TYPE (t), con)));
  3681.         }
  3682.  
  3683.           TREE_OPERAND (t, 0)
  3684.         = fold (build (code, TREE_TYPE (t), arg0, con));
  3685.           TREE_OPERAND (t, 1) = var;
  3686.           if (integer_zerop (TREE_OPERAND (t, 0))
  3687.           && TREE_CODE (t) == PLUS_EXPR)
  3688.         return convert (TREE_TYPE (t), var);
  3689.           return t;
  3690.         }
  3691.     }
  3692.     binary:
  3693. #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
  3694.       if (TREE_CODE (arg1) == REAL_CST)
  3695.     return t;
  3696. #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
  3697.       if (wins)
  3698.     t1 = const_binop (code, arg0, arg1, 0);
  3699.       if (t1 != NULL_TREE)
  3700.     {
  3701.       /* The return value should always have
  3702.          the same type as the original expression.  */
  3703.       TREE_TYPE (t1) = TREE_TYPE (t);
  3704.       return t1;
  3705.     }
  3706.       return t;
  3707.  
  3708.     case MINUS_EXPR:
  3709.       if (! FLOAT_TYPE_P (type))
  3710.     {
  3711.       if (! wins && integer_zerop (arg0))
  3712.         return build1 (NEGATE_EXPR, type, arg1);
  3713.       if (integer_zerop (arg1))
  3714.         return non_lvalue (convert (type, arg0));
  3715.  
  3716.       /* (A * C) - (B * C) -> (A-B) * C.  Since we are most concerned
  3717.          about the case where C is a constant, just try one of the
  3718.          four possibilities.  */
  3719.  
  3720.       if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
  3721.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  3722.                   TREE_OPERAND (arg1, 1), 0))
  3723.         return fold (build (MULT_EXPR, type,
  3724.                 fold (build (MINUS_EXPR, type,
  3725.                          TREE_OPERAND (arg0, 0),
  3726.                          TREE_OPERAND (arg1, 0))),
  3727.                 TREE_OPERAND (arg0, 1)));
  3728.     }
  3729.       /* Convert A - (-B) to A + B.  */
  3730.       else if (TREE_CODE (arg1) == NEGATE_EXPR)
  3731.     return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  3732.  
  3733.       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3734.            || flag_fast_math)
  3735.     {
  3736.       /* Except with IEEE floating point, 0-x equals -x.  */
  3737.       if (! wins && real_zerop (arg0))
  3738.         return build1 (NEGATE_EXPR, type, arg1);
  3739.       /* Except with IEEE floating point, x-0 equals x.  */
  3740.       if (real_zerop (arg1))
  3741.         return non_lvalue (convert (type, arg0));
  3742.     }
  3743.  
  3744.       /* Fold &x - &x.  This can happen from &x.foo - &x. 
  3745.      This is unsafe for certain floats even in non-IEEE formats.
  3746.      In IEEE, it is unsafe because it does wrong for NaNs.
  3747.      Also note that operand_equal_p is always false if an operand
  3748.      is volatile.  */
  3749.  
  3750.       if ((! FLOAT_TYPE_P (type) || flag_fast_math)
  3751.       && operand_equal_p (arg0, arg1, 0))
  3752.     return convert (type, integer_zero_node);
  3753.  
  3754.       goto associate;
  3755.  
  3756.     case MULT_EXPR:
  3757.       if (! FLOAT_TYPE_P (type))
  3758.     {
  3759.       if (integer_zerop (arg1))
  3760.         return omit_one_operand (type, arg1, arg0);
  3761.       if (integer_onep (arg1))
  3762.         return non_lvalue (convert (type, arg0));
  3763.  
  3764.       /* ((A / C) * C) is A if the division is an
  3765.          EXACT_DIV_EXPR.   Since C is normally a constant,
  3766.          just check for one of the four possibilities.  */
  3767.  
  3768.       if (TREE_CODE (arg0) == EXACT_DIV_EXPR
  3769.           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
  3770.         return TREE_OPERAND (arg0, 0);
  3771.  
  3772.       /* (a * (1 << b)) is (a << b)  */
  3773.       if (TREE_CODE (arg1) == LSHIFT_EXPR
  3774.           && integer_onep (TREE_OPERAND (arg1, 0)))
  3775.         return fold (build (LSHIFT_EXPR, type, arg0,
  3776.                 TREE_OPERAND (arg1, 1)));
  3777.       if (TREE_CODE (arg0) == LSHIFT_EXPR
  3778.           && integer_onep (TREE_OPERAND (arg0, 0)))
  3779.         return fold (build (LSHIFT_EXPR, type, arg1,
  3780.                 TREE_OPERAND (arg0, 1)));
  3781.     }
  3782.       else
  3783.     {
  3784.       /* x*0 is 0, except for IEEE floating point.  */
  3785.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3786.            || flag_fast_math)
  3787.           && real_zerop (arg1))
  3788.         return omit_one_operand (type, arg1, arg0);
  3789.       /* In IEEE floating point, x*1 is not equivalent to x for snans.
  3790.          However, ANSI says we can drop signals,
  3791.          so we can do this anyway.  */
  3792.       if (real_onep (arg1))
  3793.         return non_lvalue (convert (type, arg0));
  3794.       /* x*2 is x+x */
  3795.       if (! wins && real_twop (arg1))
  3796.         {
  3797.           tree arg = save_expr (arg0);
  3798.           return build (PLUS_EXPR, type, arg, arg);
  3799.         }
  3800.     }
  3801.       goto associate;
  3802.  
  3803.     case BIT_IOR_EXPR:
  3804.     bit_ior:
  3805.       if (integer_all_onesp (arg1))
  3806.     return omit_one_operand (type, arg1, arg0);
  3807.       if (integer_zerop (arg1))
  3808.     return non_lvalue (convert (type, arg0));
  3809.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  3810.       if (t1 != NULL_TREE)
  3811.     return t1;
  3812.  
  3813.       /* (a << C1) | (a >> C2) if A is unsigned and C1+C2 is the size of A
  3814.      is a rotate of A by C1 bits.  */
  3815.  
  3816.       if ((TREE_CODE (arg0) == RSHIFT_EXPR
  3817.        || TREE_CODE (arg0) == LSHIFT_EXPR)
  3818.       && (TREE_CODE (arg1) == RSHIFT_EXPR
  3819.           || TREE_CODE (arg1) == LSHIFT_EXPR)
  3820.       && TREE_CODE (arg0) != TREE_CODE (arg1)
  3821.       && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
  3822.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))
  3823.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  3824.       && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
  3825.       && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
  3826.       && TREE_INT_CST_HIGH (TREE_OPERAND (arg1, 1)) == 0
  3827.       && ((TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
  3828.            + TREE_INT_CST_LOW (TREE_OPERAND (arg1, 1)))
  3829.           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
  3830.     return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
  3831.               TREE_CODE (arg0) == LSHIFT_EXPR
  3832.               ? TREE_OPERAND (arg0, 1) : TREE_OPERAND (arg1, 1));
  3833.  
  3834.       goto associate;
  3835.  
  3836.     case BIT_XOR_EXPR:
  3837.       if (integer_zerop (arg1))
  3838.     return non_lvalue (convert (type, arg0));
  3839.       if (integer_all_onesp (arg1))
  3840.     return fold (build1 (BIT_NOT_EXPR, type, arg0));
  3841.       goto associate;
  3842.  
  3843.     case BIT_AND_EXPR:
  3844.     bit_and:
  3845.       if (integer_all_onesp (arg1))
  3846.     return non_lvalue (convert (type, arg0));
  3847.       if (integer_zerop (arg1))
  3848.     return omit_one_operand (type, arg1, arg0);
  3849.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  3850.       if (t1 != NULL_TREE)
  3851.     return t1;
  3852.       /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char.  */
  3853.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
  3854.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  3855.     {
  3856.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
  3857.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
  3858.           && (~TREE_INT_CST_LOW (arg0)
  3859.           & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
  3860.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
  3861.     }
  3862.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
  3863.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  3864.     {
  3865.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
  3866.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
  3867.           && (~TREE_INT_CST_LOW (arg1)
  3868.           & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
  3869.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
  3870.     }
  3871.       goto associate;
  3872.  
  3873.     case BIT_ANDTC_EXPR:
  3874.       if (integer_all_onesp (arg0))
  3875.     return non_lvalue (convert (type, arg1));
  3876.       if (integer_zerop (arg0))
  3877.     return omit_one_operand (type, arg0, arg1);
  3878.       if (TREE_CODE (arg1) == INTEGER_CST)
  3879.     {
  3880.       arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
  3881.       code = BIT_AND_EXPR;
  3882.       goto bit_and;
  3883.     }
  3884.       goto binary;
  3885.  
  3886.     case RDIV_EXPR:
  3887.       /* In most cases, do nothing with a divide by zero.  */
  3888. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3889. #ifndef REAL_INFINITY
  3890.       if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
  3891.     return t;
  3892. #endif
  3893. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3894.  
  3895.       /* In IEEE floating point, x/1 is not equivalent to x for snans.
  3896.      However, ANSI says we can drop signals, so we can do this anyway.  */
  3897.       if (real_onep (arg1))
  3898.     return non_lvalue (convert (type, arg0));
  3899.  
  3900.       /* If ARG1 is a constant, we can convert this to a multiply by the
  3901.      reciprocal.  This does not have the same rounding properties,
  3902.      so only do this if -ffast-math.  We can actually always safely
  3903.      do it if ARG1 is a power of two, but it's hard to tell if it is
  3904.      or not in a portable manner.  */
  3905.       if (TREE_CODE (arg1) == REAL_CST && flag_fast_math
  3906.       && 0 != (tem = const_binop (code, build_real (type, dconst1),
  3907.                       arg1, 0)))
  3908.     return fold (build (MULT_EXPR, type, arg0, tem));
  3909.  
  3910.       goto binary;
  3911.  
  3912.     case TRUNC_DIV_EXPR:
  3913.     case ROUND_DIV_EXPR:
  3914.     case FLOOR_DIV_EXPR:
  3915.     case CEIL_DIV_EXPR:
  3916.     case EXACT_DIV_EXPR:
  3917.       if (integer_onep (arg1))
  3918.     return non_lvalue (convert (type, arg0));
  3919.       if (integer_zerop (arg1))
  3920.     return t;
  3921.  
  3922.       /* If we have ((a / C1) / C2) where both division are the same type, try
  3923.      to simplify.  First see if C1 * C2 overflows or not.  */
  3924.       if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
  3925.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
  3926.     {
  3927.       tree new_divisor;
  3928.  
  3929.       new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
  3930.       tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
  3931.  
  3932.       if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
  3933.           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
  3934.         {
  3935.           /* If no overflow, divide by C1*C2.  */
  3936.           return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
  3937.         }
  3938.     }
  3939.  
  3940.       /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
  3941.      where C1 % C3 == 0 or C3 % C1 == 0.  We can simplify these
  3942.      expressions, which often appear in the offsets or sizes of
  3943.      objects with a varying size.  Only deal with positive divisors
  3944.      and multiplicands.   If C2 is negative, we must have C2 % C3 == 0.
  3945.  
  3946.      Look for NOPs and SAVE_EXPRs inside.  */
  3947.  
  3948.       if (TREE_CODE (arg1) == INTEGER_CST
  3949.       && tree_int_cst_sgn (arg1) >= 0)
  3950.     {
  3951.       int have_save_expr = 0;
  3952.       tree c2 = integer_zero_node;
  3953.       tree xarg0 = arg0;
  3954.  
  3955.       if (TREE_CODE (xarg0) == SAVE_EXPR)
  3956.         have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
  3957.  
  3958.       STRIP_NOPS (xarg0);
  3959.  
  3960.       if (TREE_CODE (xarg0) == PLUS_EXPR
  3961.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
  3962.         c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
  3963.       else if (TREE_CODE (xarg0) == MINUS_EXPR
  3964.            && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  3965.            /* If we are doing this computation unsigned, the negate
  3966.               is incorrect.  */
  3967.            && ! TREE_UNSIGNED (type))
  3968.         {
  3969.           c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
  3970.           xarg0 = TREE_OPERAND (xarg0, 0);
  3971.         }
  3972.  
  3973.       if (TREE_CODE (xarg0) == SAVE_EXPR)
  3974.         have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
  3975.  
  3976.       STRIP_NOPS (xarg0);
  3977.  
  3978.       if (TREE_CODE (xarg0) == MULT_EXPR
  3979.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  3980.           && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
  3981.           && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
  3982.                           TREE_OPERAND (xarg0, 1), arg1, 1))
  3983.           || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
  3984.                          TREE_OPERAND (xarg0, 1), 1)))
  3985.           && (tree_int_cst_sgn (c2) >= 0
  3986.           || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
  3987.                          arg1, 1))))
  3988.         {
  3989.           tree outer_div = integer_one_node;
  3990.           tree c1 = TREE_OPERAND (xarg0, 1);
  3991.           tree c3 = arg1;
  3992.  
  3993.           /* If C3 > C1, set them equal and do a divide by
  3994.          C3/C1 at the end of the operation.  */
  3995.           if (tree_int_cst_lt (c1, c3))
  3996.         outer_div = const_binop (code, c3, c1, 0), c3 = c1;
  3997.         
  3998.           /* The result is A * (C1/C3) + (C2/C3).  */
  3999.           t = fold (build (PLUS_EXPR, type,
  4000.                    fold (build (MULT_EXPR, type,
  4001.                         TREE_OPERAND (xarg0, 0),
  4002.                         const_binop (code, c1, c3, 1))),
  4003.                    const_binop (code, c2, c3, 1)));
  4004.  
  4005.           if (! integer_onep (outer_div))
  4006.         t = fold (build (code, type, t, convert (type, outer_div)));
  4007.  
  4008.           if (have_save_expr)
  4009.         t = save_expr (t);
  4010.  
  4011.           return t;
  4012.         }
  4013.     }
  4014.  
  4015.       goto binary;
  4016.  
  4017.     case CEIL_MOD_EXPR:
  4018.     case FLOOR_MOD_EXPR:
  4019.     case ROUND_MOD_EXPR:
  4020.     case TRUNC_MOD_EXPR:
  4021.       if (integer_onep (arg1))
  4022.     return omit_one_operand (type, integer_zero_node, arg0);
  4023.       if (integer_zerop (arg1))
  4024.     return t;
  4025.  
  4026.       /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
  4027.      where C1 % C3 == 0.  Handle similarly to the division case,
  4028.      but don't bother with SAVE_EXPRs.  */
  4029.  
  4030.       if (TREE_CODE (arg1) == INTEGER_CST
  4031.       && ! integer_zerop (arg1))
  4032.     {
  4033.       tree c2 = integer_zero_node;
  4034.       tree xarg0 = arg0;
  4035.  
  4036.       if (TREE_CODE (xarg0) == PLUS_EXPR
  4037.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
  4038.         c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
  4039.       else if (TREE_CODE (xarg0) == MINUS_EXPR
  4040.            && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  4041.            && ! TREE_UNSIGNED (type))
  4042.         {
  4043.           c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
  4044.           xarg0 = TREE_OPERAND (xarg0, 0);
  4045.         }
  4046.  
  4047.       STRIP_NOPS (xarg0);
  4048.  
  4049.       if (TREE_CODE (xarg0) == MULT_EXPR
  4050.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  4051.           && integer_zerop (const_binop (TRUNC_MOD_EXPR,
  4052.                          TREE_OPERAND (xarg0, 1),
  4053.                          arg1, 1))
  4054.           && tree_int_cst_sgn (c2) >= 0)
  4055.         /* The result is (C2%C3).  */
  4056.         return omit_one_operand (type, const_binop (code, c2, arg1, 1),
  4057.                      TREE_OPERAND (xarg0, 0));
  4058.     }
  4059.  
  4060.       goto binary;
  4061.  
  4062.     case LSHIFT_EXPR:
  4063.     case RSHIFT_EXPR:
  4064.     case LROTATE_EXPR:
  4065.     case RROTATE_EXPR:
  4066.       if (integer_zerop (arg1))
  4067.     return non_lvalue (convert (type, arg0));
  4068.       /* Since negative shift count is not well-defined,
  4069.      don't try to compute it in the compiler.  */
  4070.       if (tree_int_cst_sgn (arg1) < 0)
  4071.     return t;
  4072.       goto binary;
  4073.  
  4074.     case MIN_EXPR:
  4075.       if (operand_equal_p (arg0, arg1, 0))
  4076.     return arg0;
  4077.       if (INTEGRAL_TYPE_P (type)
  4078.       && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
  4079.     return omit_one_operand (type, arg1, arg0);
  4080.       goto associate;
  4081.  
  4082.     case MAX_EXPR:
  4083.       if (operand_equal_p (arg0, arg1, 0))
  4084.     return arg0;
  4085.       if (INTEGRAL_TYPE_P (type)
  4086.       && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
  4087.     return omit_one_operand (type, arg1, arg0);
  4088.       goto associate;
  4089.  
  4090.     case TRUTH_NOT_EXPR:
  4091.       /* Note that the operand of this must be an int
  4092.      and its values must be 0 or 1.
  4093.      ("true" is a fixed value perhaps depending on the language,
  4094.      but we don't handle values other than 1 correctly yet.)  */
  4095.       return invert_truthvalue (arg0);
  4096.  
  4097.     case TRUTH_ANDIF_EXPR:
  4098.       /* Note that the operands of this must be ints
  4099.      and their values must be 0 or 1.
  4100.      ("true" is a fixed value perhaps depending on the language.)  */
  4101.       /* If first arg is constant zero, return it.  */
  4102.       if (integer_zerop (arg0))
  4103.     return arg0;
  4104.     case TRUTH_AND_EXPR:
  4105.       /* If either arg is constant true, drop it.  */
  4106.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  4107.     return non_lvalue (arg1);
  4108.       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
  4109.     return non_lvalue (arg0);
  4110.       /* If second arg is constant zero, result is zero, but first arg
  4111.      must be evaluated.  */
  4112.       if (integer_zerop (arg1))
  4113.     return omit_one_operand (type, arg1, arg0);
  4114.  
  4115.     truth_andor:
  4116.       /* We only do these simplifications if we are optimizing.  */
  4117.       if (!optimize)
  4118.     return t;
  4119.  
  4120.       /* Check for things like (A || B) && (A || C).  We can convert this
  4121.      to A || (B && C).  Note that either operator can be any of the four
  4122.      truth and/or operations and the transformation will still be
  4123.      valid.   Also note that we only care about order for the
  4124.      ANDIF and ORIF operators.  */
  4125.       if (TREE_CODE (arg0) == TREE_CODE (arg1)
  4126.       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
  4127.           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
  4128.           || TREE_CODE (arg0) == TRUTH_AND_EXPR
  4129.           || TREE_CODE (arg0) == TRUTH_OR_EXPR))
  4130.     {
  4131.       tree a00 = TREE_OPERAND (arg0, 0);
  4132.       tree a01 = TREE_OPERAND (arg0, 1);
  4133.       tree a10 = TREE_OPERAND (arg1, 0);
  4134.       tree a11 = TREE_OPERAND (arg1, 1);
  4135.       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
  4136.                   || TREE_CODE (arg0) == TRUTH_AND_EXPR)
  4137.                  && (code == TRUTH_AND_EXPR
  4138.                  || code == TRUTH_OR_EXPR));
  4139.  
  4140.       if (operand_equal_p (a00, a10, 0))
  4141.         return fold (build (TREE_CODE (arg0), type, a00,
  4142.                 fold (build (code, type, a01, a11))));
  4143.       else if (commutative && operand_equal_p (a00, a11, 0))
  4144.         return fold (build (TREE_CODE (arg0), type, a00,
  4145.                 fold (build (code, type, a01, a10))));
  4146.       else if (commutative && operand_equal_p (a01, a10, 0))
  4147.         return fold (build (TREE_CODE (arg0), type, a01,
  4148.                 fold (build (code, type, a00, a11))));
  4149.  
  4150.       /* This case if tricky because we must either have commutative
  4151.          operators or else A10 must not have side-effects.  */
  4152.  
  4153.       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
  4154.            && operand_equal_p (a01, a11, 0))
  4155.         return fold (build (TREE_CODE (arg0), type,
  4156.                 fold (build (code, type, a00, a10)),
  4157.                 a01));
  4158.     }
  4159.  
  4160.       /* Check for the possibility of merging component references.  If our
  4161.      lhs is another similar operation, try to merge its rhs with our
  4162.      rhs.  Then try to merge our lhs and rhs.  */
  4163.       if (TREE_CODE (arg0) == code
  4164.       && 0 != (tem = fold_truthop (code, type,
  4165.                        TREE_OPERAND (arg0, 1), arg1)))
  4166.     return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
  4167.  
  4168.       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
  4169.     return tem;
  4170.  
  4171.       return t;
  4172.  
  4173.     case TRUTH_ORIF_EXPR:
  4174.       /* Note that the operands of this must be ints
  4175.      and their values must be 0 or true.
  4176.      ("true" is a fixed value perhaps depending on the language.)  */
  4177.       /* If first arg is constant true, return it.  */
  4178.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  4179.     return arg0;
  4180.     case TRUTH_OR_EXPR:
  4181.       /* If either arg is constant zero, drop it.  */
  4182.       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
  4183.     return non_lvalue (arg1);
  4184.       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
  4185.     return non_lvalue (arg0);
  4186.       /* If second arg is constant true, result is true, but we must
  4187.      evaluate first arg.  */
  4188.       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
  4189.     return omit_one_operand (type, arg1, arg0);
  4190.       goto truth_andor;
  4191.  
  4192.     case TRUTH_XOR_EXPR:
  4193.       /* If either arg is constant zero, drop it.  */
  4194.       if (integer_zerop (arg0))
  4195.     return non_lvalue (arg1);
  4196.       if (integer_zerop (arg1))
  4197.     return non_lvalue (arg0);
  4198.       /* If either arg is constant true, this is a logical inversion.  */
  4199.       if (integer_onep (arg0))
  4200.     return non_lvalue (invert_truthvalue (arg1));
  4201.       if (integer_onep (arg1))
  4202.     return non_lvalue (invert_truthvalue (arg0));
  4203.       return t;
  4204.  
  4205.     case EQ_EXPR:
  4206.     case NE_EXPR:
  4207.     case LT_EXPR:
  4208.     case GT_EXPR:
  4209.     case LE_EXPR:
  4210.     case GE_EXPR:
  4211.       /* If one arg is a constant integer, put it last.  */
  4212.       if (TREE_CODE (arg0) == INTEGER_CST
  4213.       && TREE_CODE (arg1) != INTEGER_CST)
  4214.     {
  4215.       TREE_OPERAND (t, 0) = arg1;
  4216.       TREE_OPERAND (t, 1) = arg0;
  4217.       arg0 = TREE_OPERAND (t, 0);
  4218.       arg1 = TREE_OPERAND (t, 1);
  4219.       code = swap_tree_comparison (code);
  4220.       TREE_SET_CODE (t, code);
  4221.     }
  4222.  
  4223.       /* Convert foo++ == CONST into ++foo == CONST + INCR.
  4224.      First, see if one arg is constant; find the constant arg
  4225.      and the other one.  */
  4226.       {
  4227.     tree constop = 0, varop;
  4228.     tree *constoploc;
  4229.  
  4230.     if (TREE_CONSTANT (arg1))
  4231.       constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
  4232.     if (TREE_CONSTANT (arg0))
  4233.       constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
  4234.  
  4235.     if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
  4236.       {
  4237.         /* This optimization is invalid for ordered comparisons
  4238.            if CONST+INCR overflows or if foo+incr might overflow.
  4239.            This optimization is invalid for floating point due to rounding.
  4240.            For pointer types we assume overflow doesn't happen.  */
  4241.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  4242.         || (! FLOAT_TYPE_P (TREE_TYPE (varop))
  4243.             && (code == EQ_EXPR || code == NE_EXPR)))
  4244.           {
  4245.         tree newconst
  4246.           = fold (build (PLUS_EXPR, TREE_TYPE (varop),
  4247.                  constop, TREE_OPERAND (varop, 1)));
  4248.         TREE_SET_CODE (varop, PREINCREMENT_EXPR);
  4249.         *constoploc = newconst;
  4250.         return t;
  4251.           }
  4252.       }
  4253.     else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
  4254.       {
  4255.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  4256.         || (! FLOAT_TYPE_P (TREE_TYPE (varop))
  4257.             && (code == EQ_EXPR || code == NE_EXPR)))
  4258.           {
  4259.         tree newconst
  4260.           = fold (build (MINUS_EXPR, TREE_TYPE (varop),
  4261.                  constop, TREE_OPERAND (varop, 1)));
  4262.         TREE_SET_CODE (varop, PREDECREMENT_EXPR);
  4263.         *constoploc = newconst;
  4264.         return t;
  4265.           }
  4266.       }
  4267.       }
  4268.  
  4269.       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
  4270.       if (TREE_CODE (arg1) == INTEGER_CST
  4271.       && TREE_CODE (arg0) != INTEGER_CST
  4272.       && tree_int_cst_sgn (arg1) > 0)
  4273.     {
  4274.       switch (TREE_CODE (t))
  4275.         {
  4276.         case GE_EXPR:
  4277.           code = GT_EXPR;
  4278.           TREE_SET_CODE (t, code);
  4279.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
  4280.           TREE_OPERAND (t, 1) = arg1;
  4281.           break;
  4282.  
  4283.         case LT_EXPR:
  4284.           code = LE_EXPR;
  4285.           TREE_SET_CODE (t, code);
  4286.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
  4287.           TREE_OPERAND (t, 1) = arg1;
  4288.         }
  4289.     }
  4290.  
  4291.       /* If this is an EQ or NE comparison with zero and ARG0 is
  4292.      (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
  4293.      two operations, but the latter can be done in one less insn
  4294.      one machine that have only two-operand insns or on which a
  4295.      constant cannot be the first operand.  */
  4296.       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
  4297.       && TREE_CODE (arg0) == BIT_AND_EXPR)
  4298.     {
  4299.       if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
  4300.           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
  4301.         return
  4302.           fold (build (code, type,
  4303.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  4304.                   build (RSHIFT_EXPR,
  4305.                      TREE_TYPE (TREE_OPERAND (arg0, 0)),
  4306.                      TREE_OPERAND (arg0, 1),
  4307.                      TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
  4308.                   convert (TREE_TYPE (arg0),
  4309.                        integer_one_node)),
  4310.                arg1));
  4311.       else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
  4312.            && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
  4313.         return
  4314.           fold (build (code, type,
  4315.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  4316.                   build (RSHIFT_EXPR,
  4317.                      TREE_TYPE (TREE_OPERAND (arg0, 1)),
  4318.                      TREE_OPERAND (arg0, 0),
  4319.                      TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
  4320.                   convert (TREE_TYPE (arg0),
  4321.                        integer_one_node)),
  4322.                arg1));
  4323.     }
  4324.  
  4325.       /* If this is an NE or EQ comparison of zero against the result of a
  4326.      signed MOD operation whose second operand is a power of 2, make
  4327.      the MOD operation unsigned since it is simpler and equivalent.  */
  4328.       if ((code == NE_EXPR || code == EQ_EXPR)
  4329.       && integer_zerop (arg1)
  4330.       && ! TREE_UNSIGNED (TREE_TYPE (arg0))
  4331.       && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
  4332.           || TREE_CODE (arg0) == CEIL_MOD_EXPR
  4333.           || TREE_CODE (arg0) == FLOOR_MOD_EXPR
  4334.           || TREE_CODE (arg0) == ROUND_MOD_EXPR)
  4335.       && integer_pow2p (TREE_OPERAND (arg0, 1)))
  4336.     {
  4337.       tree newtype = unsigned_type (TREE_TYPE (arg0));
  4338.       tree newmod = build (TREE_CODE (arg0), newtype,
  4339.                    convert (newtype, TREE_OPERAND (arg0, 0)),
  4340.                    convert (newtype, TREE_OPERAND (arg0, 1)));
  4341.  
  4342.       return build (code, type, newmod, convert (newtype, arg1));
  4343.     }
  4344.  
  4345.       /* If this is an NE comparison of zero with an AND of one, remove the
  4346.      comparison since the AND will give the correct value.  */
  4347.       if (code == NE_EXPR && integer_zerop (arg1)
  4348.       && TREE_CODE (arg0) == BIT_AND_EXPR
  4349.       && integer_onep (TREE_OPERAND (arg0, 1)))
  4350.     return convert (type, arg0);
  4351.  
  4352.       /* If we have (A & C) == C where C is a power of 2, convert this into
  4353.      (A & C) != 0.  Similarly for NE_EXPR.  */
  4354.       if ((code == EQ_EXPR || code == NE_EXPR)
  4355.       && TREE_CODE (arg0) == BIT_AND_EXPR
  4356.       && integer_pow2p (TREE_OPERAND (arg0, 1))
  4357.       && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
  4358.     return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
  4359.               arg0, integer_zero_node);
  4360.  
  4361.       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
  4362.      and similarly for >= into !=.  */
  4363.       if ((code == LT_EXPR || code == GE_EXPR)
  4364.       && TREE_UNSIGNED (TREE_TYPE (arg0))
  4365.       && TREE_CODE (arg1) == LSHIFT_EXPR
  4366.       && integer_onep (TREE_OPERAND (arg1, 0)))
  4367.     return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, 
  4368.               build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
  4369.                  TREE_OPERAND (arg1, 1)),
  4370.               convert (TREE_TYPE (arg0), integer_zero_node));
  4371.  
  4372.       else if ((code == LT_EXPR || code == GE_EXPR)
  4373.            && TREE_UNSIGNED (TREE_TYPE (arg0))
  4374.            && (TREE_CODE (arg1) == NOP_EXPR
  4375.            || TREE_CODE (arg1) == CONVERT_EXPR)
  4376.            && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
  4377.            && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
  4378.     return
  4379.       build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
  4380.          convert (TREE_TYPE (arg0),
  4381.               build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
  4382.                  TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
  4383.          convert (TREE_TYPE (arg0), integer_zero_node));
  4384.  
  4385.       /* Simplify comparison of something with itself.  (For IEEE
  4386.      floating-point, we can only do some of these simplifications.)  */
  4387.       if (operand_equal_p (arg0, arg1, 0))
  4388.     {
  4389.       switch (code)
  4390.         {
  4391.         case EQ_EXPR:
  4392.         case GE_EXPR:
  4393.         case LE_EXPR:
  4394.           if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  4395.         {
  4396.           t = build_int_2 (1, 0);
  4397.           TREE_TYPE (t) = type;
  4398.           return t;
  4399.         }
  4400.           code = EQ_EXPR;
  4401.           TREE_SET_CODE (t, code);
  4402.           break;
  4403.  
  4404.         case NE_EXPR:
  4405.           /* For NE, we can only do this simplification if integer.  */
  4406.           if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  4407.         break;
  4408.           /* ... fall through ... */
  4409.         case GT_EXPR:
  4410.         case LT_EXPR:
  4411.           t = build_int_2 (0, 0);
  4412.           TREE_TYPE (t) = type;
  4413.           return t;
  4414.         }
  4415.     }
  4416.  
  4417.       /* An unsigned comparison against 0 can be simplified.  */
  4418.       if (integer_zerop (arg1)
  4419.       && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
  4420.           || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
  4421.       && TREE_UNSIGNED (TREE_TYPE (arg1)))
  4422.     {
  4423.       switch (TREE_CODE (t))
  4424.         {
  4425.         case GT_EXPR:
  4426.           code = NE_EXPR;
  4427.           TREE_SET_CODE (t, NE_EXPR);
  4428.           break;
  4429.         case LE_EXPR:
  4430.           code = EQ_EXPR;
  4431.           TREE_SET_CODE (t, EQ_EXPR);
  4432.           break;
  4433.         case GE_EXPR:
  4434.           return omit_one_operand (type,
  4435.                        convert (type, integer_one_node),
  4436.                        arg0);
  4437.         case LT_EXPR:
  4438.           return omit_one_operand (type,
  4439.                        convert (type, integer_zero_node),
  4440.                        arg0);
  4441.         }
  4442.     }
  4443.  
  4444.       /* If we are comparing an expression that just has comparisons
  4445.      of two integer values, arithmetic expressions of those comparisons,
  4446.      and constants, we can simplify it.  There are only three cases
  4447.      to check: the two values can either be equal, the first can be
  4448.      greater, or the second can be greater.  Fold the expression for
  4449.      those three values.  Since each value must be 0 or 1, we have
  4450.      eight possibilities, each of which corresponds to the constant 0
  4451.      or 1 or one of the six possible comparisons.
  4452.  
  4453.      This handles common cases like (a > b) == 0 but also handles
  4454.      expressions like  ((x > y) - (y > x)) > 0, which supposedly
  4455.      occur in macroized code.  */
  4456.  
  4457.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
  4458.     {
  4459.       tree cval1 = 0, cval2 = 0;
  4460.       int save_p = 0;
  4461.  
  4462.       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
  4463.           /* Don't handle degenerate cases here; they should already
  4464.          have been handled anyway.  */
  4465.           && cval1 != 0 && cval2 != 0
  4466.           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
  4467.           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
  4468.           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
  4469.           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
  4470.                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
  4471.         {
  4472.           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
  4473.           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
  4474.  
  4475.           /* We can't just pass T to eval_subst in case cval1 or cval2
  4476.          was the same as ARG1.  */
  4477.  
  4478.           tree high_result
  4479.         = fold (build (code, type,
  4480.                    eval_subst (arg0, cval1, maxval, cval2, minval),
  4481.                    arg1));
  4482.           tree equal_result
  4483.         = fold (build (code, type,
  4484.                    eval_subst (arg0, cval1, maxval, cval2, maxval),
  4485.                    arg1));
  4486.           tree low_result
  4487.         = fold (build (code, type,
  4488.                    eval_subst (arg0, cval1, minval, cval2, maxval),
  4489.                    arg1));
  4490.  
  4491.           /* All three of these results should be 0 or 1.  Confirm they
  4492.          are.  Then use those values to select the proper code
  4493.          to use.  */
  4494.  
  4495.           if ((integer_zerop (high_result)
  4496.            || integer_onep (high_result))
  4497.           && (integer_zerop (equal_result)
  4498.               || integer_onep (equal_result))
  4499.           && (integer_zerop (low_result)
  4500.               || integer_onep (low_result)))
  4501.         {
  4502.           /* Make a 3-bit mask with the high-order bit being the
  4503.              value for `>', the next for '=', and the low for '<'.  */
  4504.           switch ((integer_onep (high_result) * 4)
  4505.               + (integer_onep (equal_result) * 2)
  4506.               + integer_onep (low_result))
  4507.             {
  4508.             case 0:
  4509.               /* Always false.  */
  4510.               return omit_one_operand (type, integer_zero_node, arg0);
  4511.             case 1:
  4512.               code = LT_EXPR;
  4513.               break;
  4514.             case 2:
  4515.               code = EQ_EXPR;
  4516.               break;
  4517.             case 3:
  4518.               code = LE_EXPR;
  4519.               break;
  4520.             case 4:
  4521.               code = GT_EXPR;
  4522.               break;
  4523.             case 5:
  4524.               code = NE_EXPR;
  4525.               break;
  4526.             case 6:
  4527.               code = GE_EXPR;
  4528.               break;
  4529.             case 7:
  4530.               /* Always true.  */
  4531.               return omit_one_operand (type, integer_one_node, arg0);
  4532.             }
  4533.  
  4534.           t = build (code, type, cval1, cval2);
  4535.           if (save_p)
  4536.             return save_expr (t);
  4537.           else
  4538.             return fold (t);
  4539.         }
  4540.         }
  4541.     }
  4542.  
  4543.       /* If this is a comparison of a field, we may be able to simplify it.  */
  4544.       if ((TREE_CODE (arg0) == COMPONENT_REF
  4545.         || TREE_CODE (arg0) == BIT_FIELD_REF)
  4546.            && (code == EQ_EXPR || code == NE_EXPR)
  4547.            /* Handle the constant case even without -O
  4548.           to make sure the warnings are given.  */
  4549.            && (optimize || TREE_CODE (arg1) == INTEGER_CST))
  4550.     {
  4551.       t1 = optimize_bit_field_compare (code, type, arg0, arg1);
  4552.       return t1 ? t1 : t;
  4553.     }
  4554.  
  4555.       /* If this is a comparison of complex values and either or both
  4556.      sizes are a COMPLEX_EXPR, it is best to split up the comparisons
  4557.      and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.  This
  4558.      may prevent needless evaluations.  */
  4559.       if ((code == EQ_EXPR || code == NE_EXPR)
  4560.       && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
  4561.       && (TREE_CODE (arg0) == COMPLEX_EXPR
  4562.           || TREE_CODE (arg1) == COMPLEX_EXPR))
  4563.     {
  4564.       tree subtype = TREE_TYPE (TREE_TYPE (arg0));
  4565.       tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
  4566.       tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
  4567.       tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
  4568.       tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
  4569.  
  4570.       return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
  4571.                    : TRUTH_ORIF_EXPR),
  4572.                   type,
  4573.                   fold (build (code, type, real0, real1)),
  4574.                   fold (build (code, type, imag0, imag1))));
  4575.     }
  4576.  
  4577.       /* From here on, the only cases we handle are when the result is
  4578.      known to be a constant.
  4579.  
  4580.      To compute GT, swap the arguments and do LT.
  4581.      To compute GE, do LT and invert the result.
  4582.      To compute LE, swap the arguments, do LT and invert the result.
  4583.      To compute NE, do EQ and invert the result.
  4584.  
  4585.      Therefore, the code below must handle only EQ and LT.  */
  4586.  
  4587.       if (code == LE_EXPR || code == GT_EXPR)
  4588.     {
  4589.       tem = arg0, arg0 = arg1, arg1 = tem;
  4590.       code = swap_tree_comparison (code);
  4591.     }
  4592.  
  4593.       /* Note that it is safe to invert for real values here because we
  4594.      will check below in the one case that it matters.  */
  4595.  
  4596.       invert = 0;
  4597.       if (code == NE_EXPR || code == GE_EXPR)
  4598.     {
  4599.       invert = 1;
  4600.       code = invert_tree_comparison (code);
  4601.     }
  4602.  
  4603.       /* Compute a result for LT or EQ if args permit;
  4604.      otherwise return T.  */
  4605.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  4606.     {
  4607.       if (code == EQ_EXPR)
  4608.         t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
  4609.                    == TREE_INT_CST_LOW (arg1))
  4610.                   && (TREE_INT_CST_HIGH (arg0)
  4611.                   == TREE_INT_CST_HIGH (arg1)),
  4612.                   0);
  4613.       else
  4614.         t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
  4615.                    ? INT_CST_LT_UNSIGNED (arg0, arg1)
  4616.                    : INT_CST_LT (arg0, arg1)),
  4617.                   0);
  4618.     }
  4619.  
  4620.       /* Assume a nonexplicit constant cannot equal an explicit one,
  4621.      since such code would be undefined anyway.
  4622.      Exception: on sysvr4, using #pragma weak,
  4623.      a label can come out as 0.  */
  4624.       else if (TREE_CODE (arg1) == INTEGER_CST
  4625.            && !integer_zerop (arg1)
  4626.            && TREE_CONSTANT (arg0)
  4627.            && TREE_CODE (arg0) == ADDR_EXPR
  4628.            && code == EQ_EXPR)
  4629.     t1 = build_int_2 (0, 0);
  4630.  
  4631.       /* Two real constants can be compared explicitly.  */
  4632.       else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
  4633.     {
  4634.       /* If either operand is a NaN, the result is false with two
  4635.          exceptions: First, an NE_EXPR is true on NaNs, but that case
  4636.          is already handled correctly since we will be inverting the
  4637.          result for NE_EXPR.  Second, if we had inverted a LE_EXPR
  4638.          or a GE_EXPR into a LT_EXPR, we must return true so that it
  4639.          will be inverted into false.  */
  4640.  
  4641.       if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
  4642.           || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
  4643.         t1 = build_int_2 (invert && code == LT_EXPR, 0);
  4644.  
  4645.       else if (code == EQ_EXPR)
  4646.         t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
  4647.                          TREE_REAL_CST (arg1)),
  4648.                   0);
  4649.       else
  4650.         t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
  4651.                         TREE_REAL_CST (arg1)),
  4652.                   0);
  4653.     }
  4654.  
  4655.       if (t1 == NULL_TREE)
  4656.     return t;
  4657.  
  4658.       if (invert)
  4659.     TREE_INT_CST_LOW (t1) ^= 1;
  4660.  
  4661.       TREE_TYPE (t1) = type;
  4662.       return t1;
  4663.  
  4664.     case COND_EXPR:
  4665.       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
  4666.      so all simple results must be passed through pedantic_non_lvalue.  */
  4667.       if (TREE_CODE (arg0) == INTEGER_CST)
  4668.     return pedantic_non_lvalue
  4669.       (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
  4670.       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
  4671.     return pedantic_non_lvalue (omit_one_operand (type, arg1, arg0));
  4672.  
  4673.       /* If the second operand is zero, invert the comparison and swap
  4674.      the second and third operands.  Likewise if the second operand
  4675.      is constant and the third is not or if the third operand is
  4676.      equivalent to the first operand of the comparison.  */
  4677.  
  4678.       if (integer_zerop (arg1)
  4679.       || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
  4680.       || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
  4681.           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
  4682.                          TREE_OPERAND (t, 2),
  4683.                          TREE_OPERAND (arg0, 1))))
  4684.     {
  4685.       /* See if this can be inverted.  If it can't, possibly because
  4686.          it was a floating-point inequality comparison, don't do
  4687.          anything.  */
  4688.       tem = invert_truthvalue (arg0);
  4689.  
  4690.       if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
  4691.         {
  4692.           arg0 = TREE_OPERAND (t, 0) = tem;
  4693.           TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2);
  4694.           TREE_OPERAND (t, 2) = arg1;
  4695.           arg1 = TREE_OPERAND (t, 1);
  4696.         }
  4697.     }
  4698.  
  4699.       /* If we have A op B ? A : C, we may be able to convert this to a
  4700.      simpler expression, depending on the operation and the values
  4701.      of B and C.  IEEE floating point prevents this though,
  4702.      because A or B might be -0.0 or a NaN.  */
  4703.  
  4704.       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
  4705.       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  4706.           || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
  4707.           || flag_fast_math)
  4708.       && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
  4709.                          arg1, TREE_OPERAND (arg0, 1)))
  4710.     {
  4711.       tree arg2 = TREE_OPERAND (t, 2);
  4712.       enum tree_code comp_code = TREE_CODE (arg0);
  4713.  
  4714.       /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
  4715.          depending on the comparison operation.  */
  4716.       if (integer_zerop (TREE_OPERAND (arg0, 1))
  4717.           && TREE_CODE (arg2) == NEGATE_EXPR
  4718.           && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
  4719.         switch (comp_code)
  4720.           {
  4721.           case EQ_EXPR:
  4722.         return pedantic_non_lvalue
  4723.           (fold (build1 (NEGATE_EXPR, type, arg1)));
  4724.           case NE_EXPR:
  4725.         return pedantic_non_lvalue (convert (type, arg1));
  4726.           case GE_EXPR:
  4727.           case GT_EXPR:
  4728.         return pedantic_non_lvalue
  4729.           (fold (build1 (ABS_EXPR, type, arg1)));
  4730.           case LE_EXPR:
  4731.           case LT_EXPR:
  4732.         return pedantic_non_lvalue
  4733.           (fold (build1 (NEGATE_EXPR, type,
  4734.                  fold (build1 (ABS_EXPR, type, arg1)))));
  4735.           }
  4736.  
  4737.       /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
  4738.          always zero.  */
  4739.  
  4740.       if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
  4741.         {
  4742.           if (comp_code == NE_EXPR)
  4743.         return pedantic_non_lvalue (convert (type, arg1));
  4744.           else if (comp_code == EQ_EXPR)
  4745.         return pedantic_non_lvalue (convert (type, integer_zero_node));
  4746.         }
  4747.  
  4748.       /* If this is A op B ? A : B, this is either A, B, min (A, B),
  4749.          or max (A, B), depending on the operation.  */
  4750.  
  4751.       if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
  4752.                           arg2, TREE_OPERAND (arg0, 0)))
  4753.         switch (comp_code)
  4754.           {
  4755.           case EQ_EXPR:
  4756.         return pedantic_non_lvalue (convert (type, arg2));
  4757.           case NE_EXPR:
  4758.         return pedantic_non_lvalue (convert (type, arg1));
  4759.           case LE_EXPR:
  4760.           case LT_EXPR:
  4761.         return pedantic_non_lvalue
  4762.           (fold (build (MIN_EXPR, type, arg1, arg2)));
  4763.           case GE_EXPR:
  4764.           case GT_EXPR:
  4765.         return pedantic_non_lvalue
  4766.           (fold (build (MAX_EXPR, type, arg1, arg2)));
  4767.           }
  4768.  
  4769.       /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
  4770.          we might still be able to simplify this.  For example,
  4771.          if C1 is one less or one more than C2, this might have started
  4772.          out as a MIN or MAX and been transformed by this function.
  4773.          Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
  4774.  
  4775.       if (INTEGRAL_TYPE_P (type)
  4776.           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  4777.           && TREE_CODE (arg2) == INTEGER_CST)
  4778.         switch (comp_code)
  4779.           {
  4780.           case EQ_EXPR:
  4781.         /* We can replace A with C1 in this case.  */
  4782.         arg1 = TREE_OPERAND (t, 1)
  4783.           = convert (type, TREE_OPERAND (arg0, 1));
  4784.         break;
  4785.  
  4786.           case LT_EXPR:
  4787.         /* If C1 is C2 + 1, this is min(A, C2).  */
  4788.         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
  4789.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4790.                     const_binop (PLUS_EXPR, arg2,
  4791.                              integer_one_node, 0), 1))
  4792.           return pedantic_non_lvalue
  4793.             (fold (build (MIN_EXPR, type, arg1, arg2)));
  4794.         break;
  4795.  
  4796.           case LE_EXPR:
  4797.         /* If C1 is C2 - 1, this is min(A, C2).  */
  4798.         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
  4799.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4800.                     const_binop (MINUS_EXPR, arg2,
  4801.                              integer_one_node, 0), 1))
  4802.           return pedantic_non_lvalue
  4803.             (fold (build (MIN_EXPR, type, arg1, arg2)));
  4804.         break;
  4805.  
  4806.           case GT_EXPR:
  4807.         /* If C1 is C2 - 1, this is max(A, C2).  */
  4808.         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
  4809.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4810.                     const_binop (MINUS_EXPR, arg2,
  4811.                              integer_one_node, 0), 1))
  4812.           return pedantic_non_lvalue
  4813.             (fold (build (MAX_EXPR, type, arg1, arg2)));
  4814.         break;
  4815.  
  4816.           case GE_EXPR:
  4817.         /* If C1 is C2 + 1, this is max(A, C2).  */
  4818.         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
  4819.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4820.                     const_binop (PLUS_EXPR, arg2,
  4821.                              integer_one_node, 0), 1))
  4822.           return pedantic_non_lvalue
  4823.             (fold (build (MAX_EXPR, type, arg1, arg2)));
  4824.         break;
  4825.           }
  4826.     }
  4827.  
  4828.       /* Convert A ? 1 : 0 to simply A.  */
  4829.       if (integer_onep (TREE_OPERAND (t, 1))
  4830.       && integer_zerop (TREE_OPERAND (t, 2))
  4831.       /* If we try to convert TREE_OPERAND (t, 0) to our type, the
  4832.          call to fold will try to move the conversion inside 
  4833.          a COND, which will recurse.  In that case, the COND_EXPR
  4834.          is probably the best choice, so leave it alone.  */
  4835.       && type == TREE_TYPE (arg0))
  4836.     return pedantic_non_lvalue (arg0);
  4837.  
  4838.  
  4839.       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
  4840.      operation is simply A & 2.  */
  4841.  
  4842.       if (integer_zerop (TREE_OPERAND (t, 2))
  4843.       && TREE_CODE (arg0) == NE_EXPR
  4844.       && integer_zerop (TREE_OPERAND (arg0, 1))
  4845.       && integer_pow2p (arg1)
  4846.       && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
  4847.       && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
  4848.                   arg1, 1))
  4849.     return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
  4850.  
  4851.       return t;
  4852.  
  4853.     case COMPOUND_EXPR:
  4854.       /* When pedantic, a compound expression can be neither an lvalue
  4855.      nor an integer constant expression.  */
  4856.       if (TREE_SIDE_EFFECTS (arg0) || pedantic)
  4857.     return t;
  4858.       /* Don't let (0, 0) be null pointer constant.  */
  4859.       if (integer_zerop (arg1))
  4860.     return non_lvalue (arg1);
  4861.       return arg1;
  4862.  
  4863.     case COMPLEX_EXPR:
  4864.       if (wins)
  4865.     return build_complex (arg0, arg1);
  4866.       return t;
  4867.  
  4868.     case REALPART_EXPR:
  4869.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  4870.     return t;
  4871.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  4872.     return omit_one_operand (type, TREE_OPERAND (arg0, 0),
  4873.                  TREE_OPERAND (arg0, 1));
  4874.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  4875.     return TREE_REALPART (arg0);
  4876.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  4877.     return fold (build (TREE_CODE (arg0), type,
  4878.                 fold (build1 (REALPART_EXPR, type,
  4879.                       TREE_OPERAND (arg0, 0))),
  4880.                 fold (build1 (REALPART_EXPR,
  4881.                       type, TREE_OPERAND (arg0, 1)))));
  4882.       return t;
  4883.  
  4884.     case IMAGPART_EXPR:
  4885.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  4886.     return convert (type, integer_zero_node);
  4887.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  4888.     return omit_one_operand (type, TREE_OPERAND (arg0, 1),
  4889.                  TREE_OPERAND (arg0, 0));
  4890.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  4891.     return TREE_IMAGPART (arg0);
  4892.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  4893.     return fold (build (TREE_CODE (arg0), type,
  4894.                 fold (build1 (IMAGPART_EXPR, type,
  4895.                       TREE_OPERAND (arg0, 0))),
  4896.                 fold (build1 (IMAGPART_EXPR, type,
  4897.                       TREE_OPERAND (arg0, 1)))));
  4898.       return t;
  4899.  
  4900.     default:
  4901.       return t;
  4902.     } /* switch (code) */
  4903. }
  4904.